mod_rewrite - HTTP 主机条件失败

发布于 2024-09-26 22:50:22 字数 747 浏览 1 评论 0原文

我正在尝试进行一些 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 技术交流群。

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

发布评论

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

评论(1

爱给你人给你 2024-10-03 22:50:22

RewriteCond 仅适用于next RewriteRule 指令,因此在您的情况下,admin RewriteRule 永远没有机会执行,因为首先应用此无条件规则:

RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]

有几种方法可以解决此问题,但是最直接的方法是修改您的公共网站部分以包含第二条规则的条件:

## Public site rewrites   
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^(home)(/)?$ /index.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]

A RewriteCond only applies to the next RewriteRule directive, so in your case the admin RewriteRule never gets a chance to execute because this unconditioned rule is applied first:

RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]

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:

## Public site rewrites   
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^(home)(/)?$ /index.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.local$ [NC]
RewriteRule ^([a-z0-9+-]+)(/)?$ /$1.php [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文