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
With your shown samples and attempts; please try following .htaccess rules file. As per anubhava’s comments have added
RewriteCond
conditions before eachRewriteRule
.Please make sure to clear your browser cache before testing your URLs.
Fixes applied to OP’s attempts:
RewriteRule ^(?:1|3)example$ / [R=302,L,NC]
very first rule in above file.NC
flag in bothRewriteCond
andRewriteRule
in all of the rules.RewriteCond
to makewww.
optional eg: FROM^(www.example.com|example.com)
TO^(?:www.)?example.com$
As already mentioned, the
RewriteCond
(condition) directive itself applies only to the firstRewriteRule
that follows. TheRewriteCond
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:
Alternatively, you could temporarily prefix the hostname to the URL-path and test for this directly in the
RewriteRule
directive.For example: