I am trying to redirect all files in the folder mydomain.com/es to mydomain.com/ This works using
RedirectMatch 302 ^/es/ /index.php
However this is also redirecting the folder es/ in one of my addon domains. So myaddondomain.com/es/ is going to mydomain.com/
How do I stop this? I have searched through the forum and found these rules that people have posted, but neither of them work
RewriteRule ^myaddondomain.com/?$ - [L]
or
RewriteCond %{REQUEST_URI} !^/myaddondomain.com/$
CodePudding user response:
RedirectMatch 302 ^/es/ /index.php
If this should only apply to a particular domain (ie. hostname) then you need to use mod_rewrite (RewriteRule
/ RewriteCond
) instead and check the HTTP_HOST
in a condition. For example:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com [NC]
RewriteRule ^es/ /index.php [R,L]
Note that the URL-path matched by the RewriteRule
pattern (1st argument) in a directory context does not start with a slash.
This also allows for an optional www subdomain. Remove (www\.)?
in the CondPattern if that is not required.
Note that this redirects to /index.php
, not strictly mydomain.com/
as you stated in your description. Maybe you should be redirecting to /
instead?
Aside: As far as SEO is concerned, many-to-one redirects to the homepage will likely be seen as a soft-404.
Aside:
I have searched through the forum and found these rules that people have posted, but neither of them work
RewriteRule ^myaddondomain.com/?$ - [L]
or
RewriteCond %{REQUEST_URI} !^/myaddondomain.com/$
I would be curious where you saw these (on StackOverflow?) as they are obviously incorrect in this context.
The RewriteRule
pattern (first rule) matches against the URL-path only, so the first rule will never match.
Again, the REQUEST_URI
server variable (second rule) contains the URL-path only so the "negated" condition will always be successful.