Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritingfriendly-url

Force user to a single site when visiting over a specific domain via htaccess


I try to force a user which is coming via a specific domain example-page.com to always get redirected to /countdown

My approach was the the following:

RewriteCond %{HTTP_HOST} ^example-page\.com [NC, OR]
RewriteCond %{HTTP_HOST} ^www.example-page\.com [NC]
RewriteCond %{REQUEST_URI} !^/countdown
RewriteRule ^(.*)$ https://www.example-page.com/countdown [L,R=301]

But for some reason I always get a "Too many redirects error". I get redirected to the https://www.example-page.com/countdown but somehow the RewriteCond is triggered again but it should not because of the RewriteCond %{REQUEST_URI} !^/countdown.


Solution

  • You may try this rule:

    RewriteCond %{HTTP_HOST} ^(?:www\.)?example-page\.com [NC]
    RewriteCond %{THE_REQUEST} !\s/countdown [NC]
    RewriteRule ^ https://www.example-page.com/countdown [L,R=301]
    

    Make sure to test if after clearing your browser cache.