使用 htaccess 以一种方式重定向停放域的根目录,以另一种方式重定向所有其他 url

发布于 2024-09-15 13:10:55 字数 682 浏览 1 评论 0原文

我最近得到了一个备用域(停放),并希望它的行为如下:

  • 在请求“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 技术交流群。

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

发布评论

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

评论(1

画中仙 2024-09-22 13:10:55

问题是 L 标志可能不不像您期望的那样工作,并且当 mod_rewrite 重新检查您的规则时,RewriteCond %{REQUEST_URI} !^/$ 匹配,因为 %初始重写后,{REQUEST_URI} 更新为 /fakeroot.php

有几种不同的方法可以解决这个问题,但我相信这应该足够好,并且不需要做太多改变:

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} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]

The problem is that the L flag probably doesn't work like you expect, and when mod_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:

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} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文