Search code examples
.htaccesshttp-redirectmod-rewrite

RewriteRule configuration for multisite (only for specified domain)


I have few domains on one directory instance like for example: aaa.com, bbb.com, ccc.com, ddd.com, ...

I would like to add redirects only for specified domain URL to another domain URL:

https://aaa.com/test1 => https://bbb.com/test1

The problem is that if add it with:
Redirect 301 /test1 https://bbb.com/test1
It breaks the https://bbb.com/test1 URL with "Too many redirects" error message.

I tried something like this, but it is not working:

RewriteCond %{HTTP_HOST} ^(www\.)?aaa\.com$
RewriteRule ^/test1/?$ https://bbb.com/test1 [L,R=301]

I know how to simple solve it in PHP but I need to add in .htaccess. Thank you in advance for any advice.


Solution

  • RewriteCond %{HTTP_HOST} ^(www\.)?aaa\.com$
    RewriteRule ^/test1/?$ https://bbb.com/test1 [L,R=301]
    

    In .htaccess there is no slash prefix on the URL-path that is matched by the RewriteRule pattern. In other words, ^/test1/?$ should be ^test1/?$, since you are matching against the URL-path test1 (or test1/), not /test1. Otherwise, your rule looks OK and needs to be at the top of the .htaccess file before the existing WordPress code block.

    NB: Test first with a 302 (temporary) redirect to avoid potential caching issues in case there is an error, otherwise the "error" can be cached! Needless to say, you will need to clear your browser (and any intermediary) caches before testing.