Redirecting a primary domain but not a specific subdomain with .htaccess

I currently have:

Redirect 302 / http://www.example.com 

While I still want this redirect to happen, I do not want him to redirect them if they go to say foo.mydomain.com or any other pages in this subdomain.

How can i do this?

+6
source share
1 answer

To be more specific, you need to use a RewriteCond / RewriteRule instead of a simple Redirect directive. Make a negative match ( ! ) For foo.mydomain.com and rewrite. You can map multiple subdomains to the OR group (foo|other1|other2)

 RewriteEngine On # Redirect anything except foo.example.com, bar.example.com RewriteCond %{HTTP_HOST} !^(foo|bar)\.example\.com$ [NC] # Redirect to www.example.com, preserving the URI RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302] 

If you just want to redirect to the root directory instead of adding the entire URI through $1 , use the same RewriteCond and just do:

 # Match and redirect everything to the root of www.example.com RewriteRule ^. http://www.example.com/ [L,R=302] 
+13
source

Source: https://habr.com/ru/post/946358/


All Articles