skip to Main Content

I have trouble with redirects in .htaccess.
I have several domains (example.com, example.net, example.uk). I have set of RewriteRules for each domain. But for some reason, the redirects works for all domains. Following is my tried htaccess rules file.

RewriteCond %{HTTP_HOST} ^(www.example.com|example.com) [NC]
RewriteRule ^1example$ /  [R=302,L]
RewriteRule ^2example$ /example2  [R=302,L]
RewriteRule ^3example$ /  [R=302,L]
RewriteRule ^4example$ /example4  [R=302,L]

RewriteCond %{HTTP_HOST} ^(www.example.net|example.net) [NC]
RewriteRule ^5example$ /sgsg  [R=302,L]
RewriteRule ^6example$ /example2  [R=302,L]
RewriteRule ^7example$ /  [R=302,L]
RewriteRule ^8example$ /example44  [R=302,L]

RewriteCond %{HTTP_HOST} ^(www.example.uk|example.uk) [NC]
RewriteRule ^9example$ /  [R=302,L]
RewriteRule ^10example$ /example12  [R=302,L]
RewriteRule ^11example$ /  [R=302,L]
RewriteRule ^12example$ /example41  [R=302,L]

Do you have any idea how to separate RewriteRules only for specific domain?

2

Answers


  1. With your shown samples and attempts; please try following .htaccess rules file. As per anubhava’s comments have added RewriteCond conditions before each RewriteRule.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.com$ [NC]
    RewriteRule ^(?:1|3)example$ /  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.com$ [NC]
    RewriteRule ^(2|4)example$ /example$1  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.net$ [NC]
    RewriteRule ^5example$ /sgsg  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.net$ [NC]
    RewriteRule ^6example$ /example2  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.net$ [NC]
    RewriteRule ^7example$ /  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.net$ [NC]
    RewriteRule ^8example$ /example44  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.uk$ [NC]
    RewriteRule ^9example$ /  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.uk$ [NC]
    RewriteRule ^10example$ /example12  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.uk$ [NC]
    RewriteRule ^11example$ /  [R=302,L,NC]
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.uk$ [NC]
    RewriteRule ^12example$ /example41  [R=302,L,NC]
    

    Fixes applied to OP’s attempts:

    • Attached 2 rules into 1 eg: RewriteRule ^(?:1|3)example$ / [R=302,L,NC] very first rule in above file.
    • Added NC flag in both RewriteCond and RewriteRule in all of the rules.
    • Made use of non-capturing group in RewriteCond to make www. optional eg: FROM ^(www.example.com|example.com) TO ^(?:www.)?example.com$
    Login or Signup to reply.
  2. As already mentioned, the RewriteCond (condition) directive itself applies only to the first RewriteRule that follows. The RewriteCond directive forms part of a single rule.

    However, you can "workaround" this in various ways, without having to apply the same condition to multiple rules.

    For example:

    # Skip the following 4 rules when host is NOT "example.com"
    RewriteCond %{HTTP_HOST} !^(www.)?example.com [NC]
    RewriteRule ^ - [S=4]
    
    # These rules only apply to "example.com"
    RewriteRule ^1example$ /  [R=302,L]
    RewriteRule ^2example$ /example2  [R=302,L]
    RewriteRule ^3example$ /  [R=302,L]
    RewriteRule ^4example$ /example4  [R=302,L]
    
    # Skip the following 4 rules when host is NOT "example.net"
    RewriteCond %{HTTP_HOST} !^(www.)?example.net [NC]
    RewriteRule ^ - [S=4]
    
    # These rules only apply to "example.net"
    RewriteRule ^5example$ /sgsg  [R=302,L]
    RewriteRule ^6example$ /example2  [R=302,L]
    RewriteRule ^7example$ /  [R=302,L]
    RewriteRule ^8example$ /example44  [R=302,L]
    
    # Skip the following 4 rules when host is NOT "example.uk"
    RewriteCond %{HTTP_HOST} !^(www.)?example.uk [NC]
    RewriteRule ^ - [S=4]
    
    # These rules only apply to "example.uk"
    RewriteRule ^9example$ /  [R=302,L]
    RewriteRule ^10example$ /example12  [R=302,L]
    RewriteRule ^11example$ /  [R=302,L]
    RewriteRule ^12example$ /example41  [R=302,L]
    

    Alternatively, you could temporarily prefix the hostname to the URL-path and test for this directly in the RewriteRule directive.

    For example:

    # Prefix the hostname to the URL-path
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{HTTP_HOST} ^(?:www.)?(example.com|example.net|example.uk)
    RewriteRule ^ %1%{REQUEST_URI}
    
    RewriteRule ^example.com/1example$ /  [R=302,L]
    RewriteRule ^example.com/2example$ /example2  [R=302,L]
    RewriteRule ^example.com/3example$ /  [R=302,L]
    RewriteRule ^example.com/4example$ /example4  [R=302,L]
    
    RewriteRule ^example.net/5example$ /sgsg  [R=302,L]
    RewriteRule ^example.net/6example$ /example2  [R=302,L]
    RewriteRule ^example.net/7example$ /  [R=302,L]
    RewriteRule ^example.net/8example$ /example44  [R=302,L]
    
    RewriteRule ^example.uk/9example$ /  [R=302,L]
    RewriteRule ^example.uk/10example$ /example12  [R=302,L]
    RewriteRule ^example.uk/11example$ /  [R=302,L]
    RewriteRule ^example.uk/12example$ /example41  [R=302,L]
    
    # (OPTIONAL) Remove hostname prefix from URL-path
    RewriteRule ^[^/]+/(.*) $1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search