mod_rewrite - HTTP 主机条件失败
我正在尝试进行一些 URL 重写 - 但由于某种原因它没有按我的预期工作(我可能错过了一些非常简单的东西!)。
我有两个网站 - www.domain.local 和 admin.domain.local。使用下面的 .htaccess 文件,公共站点重写很好,但是管理站点没有按预期工作,因为它是由第一个条件拾取的(即使回显 HTTP_HOST 显示我正在查看管理站点)站点域)。
Options -MultiViews
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
## Force www
RewriteCond %{HTTP_HOST} ^domain\.local$ [NC]
RewriteRule ^(.*)$ http://www.domain.local/$1 [R=301,L]
## Public site rewrites
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^(home)(/)?$ /index.php [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]
## Admin site
RewriteCond %{HTTP_HOST} ^admin\.domain\.local$ [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /manage/$1.php [QSA,L]
我做错了什么?
非常感谢, 凯夫
I'm trying to do some URL rewriting - but for some reason it's not working as I expected (I've probably missed something really simple!).
I have two sites - www.domain.local and admin.domain.local. Using the below .htaccess file, the public site rewriting is fine, but the admin site isn't working as expected, as it is being picked up by the first condition (even though echoing out the HTTP_HOST shows I'm looking at the admin site domain).
Options -MultiViews
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
## Force www
RewriteCond %{HTTP_HOST} ^domain\.local$ [NC]
RewriteRule ^(.*)$ http://www.domain.local/$1 [R=301,L]
## Public site rewrites
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^(home)(/)?$ /index.php [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]
## Admin site
RewriteCond %{HTTP_HOST} ^admin\.domain\.local$ [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /manage/$1.php [QSA,L]
What am I doing wrong?
Many thanks,
Kev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RewriteCond
仅适用于nextRewriteRule
指令,因此在您的情况下,adminRewriteRule
永远没有机会执行,因为首先应用此无条件规则:有几种方法可以解决此问题,但是最直接的方法是修改您的公共网站部分以包含第二条规则的条件:
A
RewriteCond
only applies to the nextRewriteRule
directive, so in your case the adminRewriteRule
never gets a chance to execute because this unconditioned rule is applied first:There are a few ways to fix this, but the most straight-forward would be to modify your public site section to include the condition for the second rule: