开发者

Redirect root of parked domain in one way, all other urls in another way, using htaccess

开发者 https://www.devze.com 2023-01-13 00:32 出处:网络
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)

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.


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号