htaccess 对所有内容调用重写规则

发布于 2024-09-25 05:18:17 字数 753 浏览 6 评论 0原文

这是我的 htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Static pages
RewriteRule ^print/([0-9]+)$ index.php?action=PublicDisplayPrint&page_id=$1 [L,QSA]
RewriteRule ^email/([0-9]+)$ index.php?action=PublicDisplayEmail&page_id=$1 [L,QSA]
RewriteRule ^login$ index.php?action=PublicDisplayLogin [L,QSA]
RewriteRule ^search$ index.php?action=PublicDisplaySearch [L,QSA]
RewriteRule ^feedback$ index.php?action=PublicDisplayFeedback [L,QSA]
RewriteRule ^sitemap$ index.php?action=PublicDisplaySitemap [L,QSA]

# Dynamic pages
RewriteRule ^(.*)$ index.php?action=PublicDisplayPage&url=$1 [L,QSA]

它似乎在任何文件(例如 logo.gif)上调用动态页面规则,因此它似乎没有运行第二行和第三行。有什么想法吗?

This is my htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Static pages
RewriteRule ^print/([0-9]+)$ index.php?action=PublicDisplayPrint&page_id=$1 [L,QSA]
RewriteRule ^email/([0-9]+)$ index.php?action=PublicDisplayEmail&page_id=$1 [L,QSA]
RewriteRule ^login$ index.php?action=PublicDisplayLogin [L,QSA]
RewriteRule ^search$ index.php?action=PublicDisplaySearch [L,QSA]
RewriteRule ^feedback$ index.php?action=PublicDisplayFeedback [L,QSA]
RewriteRule ^sitemap$ index.php?action=PublicDisplaySitemap [L,QSA]

# Dynamic pages
RewriteRule ^(.*)$ index.php?action=PublicDisplayPage&url=$1 [L,QSA]

It seems to be calling the dynamic page rule on any file (logo.gif for example) so it doesn't seem to be running the 2nd and 3rd line. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我最亲爱的 2024-10-02 05:18:17

RewriteCond 指令的条件仅属于以下第一个 RewriteRule 指令< /a>.因此,在这种情况下,两个 RewriteCond 条件仅适用于第一个 RewriteRule 而不适用于其他规则,尤其不适用于最后一个规则。

因此,将 RewriteCond 指令放在最后一个 RewriteRule 指令前面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=PublicDisplayPage&url=$1 [L,QSA]

The condition of a RewriteCond directive does only belong to the first following RewriteRule directive. So in this case the two RewriteCond conditions do only apply to the first RewriteRule and not to the other rules, especially not to the last rule.

So put the RewriteCond directives immediately in front of the the last RewriteRule directive:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=PublicDisplayPage&url=$1 [L,QSA]
时光匆匆的小流年 2024-10-02 05:18:17

RewriteCond 仅适用于下一个 RewriteRule,因此您的最后 7 条规则将应用于任何请求。

您可以重复每个规则的条件,将其放在最后一个规则上,或者放置一个显式规则来按原样提供内容(这更灵活,因为允许您稍后添加更多动态规则):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
#If it's a file or directory, serve as is, without rewriting
RewriteRule ^ - [L] 

#Rest of the rules

The RewriteConds are applicable only to the next following RewriteRule so your last 7 rules will be applied to any request.

You could either repeat the condition for each rule, put it on the last rule or put an explicit rule to serve the content as is (this is more flexible because allows you to add more dynamic rules later):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
#If it's a file or directory, serve as is, without rewriting
RewriteRule ^ - [L] 

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