Tuesday, June 5, 2012

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!


Source: Tips4all

1 comment:

  1. 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

    ReplyDelete