Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect subdomain to main domain except one url

Tags:

.htaccess

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?

like image 428
AnNaMaLaI Avatar asked Aug 16 '12 13:08

AnNaMaLaI


2 Answers

Try these rules :

RewriteCond %{HTTP_HOST} ^dev\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?data
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,QSA,R=301]
like image 84
Oussama Jilal Avatar answered Oct 27 '22 09:10

Oussama Jilal


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.

like image 40
Ray Avatar answered Oct 27 '22 10:10

Ray