使用 htaccess 以一种方式重定向停放域的根目录,以另一种方式重定向所有其他 url
我最近得到了一个备用域(停放),并希望它的行为如下:
- 在请求“alt-domain.com/”(root)时:在没有重定向的情况下提供“main-domain.com/fakeroot”(200)
- 关于“alt-domain.com/anyotherpage”的请求:使用重定向(301)提供“main-domain.com/anyotherpage”
我尝试了这个,但它不起作用:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
每个规则都单独工作,但是当两者都存在,对 root 的请求也得到重定向。我尝试颠倒顺序,尝试跳过 [S=1],尝试 [NS],但没有运气。就在那时我意识到我不是 htaccess 专家,也不是正则表达式专家。
任何帮助将不胜感激。
D .
I recently got an alternate domain (parked) and would like it to behave like this:
- on a request for "alt-domain.com/" (root): serve "main-domain.com/fakeroot" without a redirect (200)
- on a request for "alt-domain.com/anyotherpage": serve "main-domain.com/anyotherpage" with a redirect (301)
I tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
Each rule works on its own, but when both are present the request for root also get a redirect. I tried inverting the order, tried skipping [S=1], tried [NS], but no luck. That's when I realized I'm not an htaccess expert, nor a regex expert.
Any help would be much appreciated.
D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
L
标志可能不不像您期望的那样工作,并且当mod_rewrite
重新检查您的规则时,RewriteCond %{REQUEST_URI} !^/$
匹配,因为%初始重写后,{REQUEST_URI}
更新为/fakeroot.php
。有几种不同的方法可以解决这个问题,但我相信这应该足够好,并且不需要做太多改变:
The problem is that the
L
flag probably doesn't work like you expect, and whenmod_rewrite
re-examines your rules,RewriteCond %{REQUEST_URI} !^/$
matches because the%{REQUEST_URI}
is updated to/fakeroot.php
after the initial rewrite.There are a few different ways to fix this, but I believe this should work well enough, and it doesn't involve changing much: