directory directory - 11 days ago 4x
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


My current htaccess redirect

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


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 (.*) [R=301,L]