AnNaMaLaI
AnNaMaLaI

Reputation: 4084

Redirect subdomain to main domain except one url

Hi I have a main domain as follows

Main domain : http://www.sample.com/

Sub domain : http://dev.sample.com/

Now If any one access http://dev.sample.com/ It needs to redirect to http://www.sample.com/ So I added the following code in my sub domain It works very well

RewriteCond %{HTTP_HOST} !www.sample.com.com$ [NC]

RewriteRule ^(.*)$ http://www.sample.com.com/$1 [L,R=301]

But from subdomain if any access the following url http://dev.sample.com/data then it should not redirect to main domain. It should stay on that sub domain page. Any one have idea?

Upvotes: 6

Views: 1758

Answers (2)

Ray
Ray

Reputation: 41428

Try this:

RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/data [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]

However, this will prevent all URL's with /data at the start of the URI. So if you had http://blog.sample.com/data it would also allow it to resolve without the rewrite. If this is a problem you can get more complex... after this rule add this to catch all non-dev subdomains:

RewriteCond %{HTTP_HOST} !dev\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]

Now if the first condition set is not met the first rule won't run, so check to make sure you're only serving dev and www up with /data.

Upvotes: 1

Oussama Jilal
Oussama Jilal

Reputation: 7739

Try these rules :

RewriteCond %{HTTP_HOST} ^dev\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?data
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,QSA,R=301]

Upvotes: 2

Related Questions