mod_rewrite 新手 - 这是正确的吗?

发布于 2024-11-25 05:33:15 字数 324 浏览 0 评论 0原文

这应该获取任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2024-12-02 05:33:15

顺便说一句,我会稍微更改此规则:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css.php|gif|jpe?g|png|css|js|json|xml|ico)$
RewriteRule ^(.*)/?$ index.php?where=$1 [QSA,L]
  1. \. 添加到 RewriteCond 模式中,以确保它仅适用于具有此类扩展名的文件(否则模式也会匹配末尾具有该文本的文件即/something/mygif <> /something/my.gif)。

  2. 在 RewriteRule 模式中将 (/) 替换为 / - 它在功能上没有区别,但在资源上稍微轻一些。


回到主题:

  • 如果“忽略整个事情,就好像网址重写已关闭”,那么很可能不支持/启用 .htaccess 文件(或者它应该具有由 AccessFileName 指令配置的不同名称:例如 <代码>访问文件名ht.access)。

    要检查它,请尝试将一些其他指令放入 .htaccess 中,看看它是否有效(例如:ErrorDocument 404 /404.phpDirectoryIndex index.php 等)。

  • 如果“每当上传包含​​上述内容的 .htaccess 时都会报告错误请求”,那么很可能不允许将这些指令放置在 .htaccess 中(需要 AllowOverride All code> 或至少 AllowOverride FileInfo; 请参阅文档)或 mod_rewrite 未启用。

    检查 Apache 的错误日志——它应该有提到这一刻的条目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文