Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess mod-rewrite to subdomain

I'm using the following to redirect wildcard sub domains to corresponding folders:

RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^(.*)$ /users/%1/$1/? [L]

I would like to add a rewrite rule that redirects anyone that accesses the direct /users/ path back to the sub-domain version like this:

www.domain.com/users/username/../../ => username.domain.com/../../

Thank you in advance!

like image 293
dSquared Avatar asked Sep 29 '11 12:09

dSquared


1 Answers

Something like this:

RewriteEngine On

RewriteCond %{http_host} ^domain.com [nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]

Do you have other rules than the one listed on the question? if yes, put these before the other one.

eg:

http://www.domain.com/users/abc?q=test => http://abc.domain.com/?q=test
http://www.domain.com/users/abc/sub1/sub2 => http://abc.domain.com/sub1/sub2
http://www.domain.com/users/abc/sub1/?q=test => http://abc.domain.com/sub1/?q=test
like image 126
Book Of Zeus Avatar answered Oct 29 '22 05:10

Book Of Zeus