mod_rewrite 新手 - 这是正确的吗?
这应该获取任何 uri 并将其作为查询字符串的一部分发送到将处理它的脚本。
RewriteEngine On
RewriteCond %{REQUEST_URI} !(css.php|gif|jpe?g|png|css|js|json|xml|ico)$
RewriteRule ^(.*)(/)?$ index.php?where=$1 [QSA,L]
我这么问是因为它在某些服务器上有效,但在其他服务器上无效。在某些平台上,它会忽略整个事情,就好像 url 重写已关闭一样,而在其他平台上,每当上传包含上述内容的 .htaccess 时,它都会报告错误请求。
This is supposed 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺便说一句,我会稍微更改此规则:
将
\.
添加到 RewriteCond 模式中,以确保它仅适用于具有此类扩展名的文件(否则模式也会匹配末尾具有该文本的文件即/something/mygif
<>/something/my.gif
)。在 RewriteRule 模式中将
(/)
替换为/
- 它在功能上没有区别,但在资源上稍微轻一些。回到主题:
如果“忽略整个事情,就好像网址重写已关闭”,那么很可能不支持/启用 .htaccess 文件(或者它应该具有由 AccessFileName 指令配置的不同名称:例如 <代码>访问文件名ht.access)。
要检查它,请尝试将一些其他指令放入 .htaccess 中,看看它是否有效(例如:
ErrorDocument 404 /404.php
或DirectoryIndex index.php
等)。如果“每当上传包含上述内容的 .htaccess 时都会报告错误请求”,那么很可能不允许将这些指令放置在 .htaccess 中(需要
AllowOverride All code> 或至少
AllowOverride FileInfo
; 请参阅文档)或 mod_rewrite 未启用。检查 Apache 的错误日志——它应该有提到这一刻的条目。
BTH I would change this rule a bit:
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
).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
orDirectoryIndex 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 leastAllowOverride FileInfo
; see docs) or mod_rewrite is not enabled.Check Apache's error log -- it should have entries mentioning this moment.