directory directory - 1 month ago 5
Apache Configuration Question

htaccess Redirect subdomain only without a path

I am redirecting the follow domain examples.

example.domain.tld to domain.tld/foo/example-foo.html


This works correct, but I need to use another approach when the same subdomain has a path including a specific slug. So I want to prevent with the current redirection I wrote the redirection on this situation

example.domain.tld/foo/


My current htaccess redirect

RewriteCond %{HTTP_HOST} !^(www|staging)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.de [NC]
RewriteCond %{REQUEST_URI} !^(foo|foo2)[NC] # <== not working
RewriteRule (.*) https://www.%2.de/foo/%1-foo.html [R=301,L]

Answer

REQUEST_URI must start with / and you need to keep capturing conditions in the end.

This should work:

RewriteCond %{HTTP_HOST} !^(www|staging)\. [NC]
RewriteCond %{REQUEST_URI} !(^|/)(foo|foo2) [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.de [NC]
RewriteRule (.*) https://www.%2.de/foo/%1-foo.html [R=301,L]
Comments