Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting main domain but not certain subdomain with .htaccess

Currently I have:

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

Whilst I still want this redirect to happen, I don't want it to redirect them if they go to say foo.mydomain.com or any other pages on this sub-domain.

How can I do this?

like image 312
Brett Avatar asked Jun 02 '13 19:06

Brett


People also ask

Can you redirect subdomain to main domain?

If you want to redirect your subdomain to your main domain, you can do this by editing your . htaccess file. Add the following lines of code to the file, replacing example.com with your actual domain name: RewriteEngine On RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC] RewriteRule ^(.

How can I redirect and rewrite my urls with an .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

Can I redirect a domain to a specific URL?

Under the Domain category, choose the Redirects menu. You'll see the Create a Redirect section. Here, you'll need to fill in which URL you want to Redirect and where you want it to Redirect To. Make sure your information is correct and choose the right connection protocol – HTTP or HTTPS.


1 Answers

To be more specific in that manner, you'll need to use RewriteCond / RewriteRule rather than a simple Redirect directive. Do a negative match (!) for foo.mydomain.com and perform the rewrite. You may match multiple subdomains with an 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 instead of appending the entire URI via $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]
like image 111
Michael Berkowski Avatar answered Oct 23 '22 15:10

Michael Berkowski