开发者

mod_rewrite newbie - is this correct?

This is suppose开发者_JS百科d to take any uri and send it as part of a query string to a script that will handle it.

RewriteEngine On
RewriteCond %{REQUEST_URI} !(css.php|gif|jpe?g|png|css|js|json|xml|ico)$
RewriteRule ^(.*)(/)?$ index.php?where=$1 [QSA,L]

I'm asking because it works on some servers and not on others. On some it just disregards the whole thing as if url rewriting is off, and on others it reports a bad request whenever .htaccess with the above content is uploaded.


BTH I would change this rule a bit:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css.php|gif|jpe?g|png|css|js|json|xml|ico)$
RewriteRule ^(.*)/?$ index.php?where=$1 [QSA,L]
  1. Added \. into RewriteCond pattern to ensure that it only works for files with such EXTENSIONS (otherwise pattern will also match files that have that text at the end i.e. /something/mygif <> /something/my.gif).

  2. Replaced (/) by / in RewriteRule pattern -- it makes no difference in functionality but a bit lighter on resources.


Back to main topic:

  • If it "disregards the whole thing as if url rewriting is off" then most likely .htaccess files are not supported/enabled (or it should have different name as configured by AccessFileName directive: e.g. AccessFileName ht.access).

    To check it try placing some other directives into .htaccess and see if it works (like: ErrorDocument 404 /404.php or DirectoryIndex index.php etc).

  • If it "reports a bad request whenever .htaccess with the above content is uploaded" then most likely these directives are not allowed to be placed in .htaccess (requires AllowOverride All or at least AllowOverride FileInfo; see docs) or mod_rewrite is not enabled.

    Check Apache's error log -- it should have entries mentioning this moment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜