Htaccess mod-rewrite to subdomain

I use the following to redirect subdomains of subdirectories to the appropriate 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 all who access the direct / users / path back to the subdomain version, like this:

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

Thank you in advance!

+6
source share
1 answer

Something like that:

 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 any other rules besides those listed in the question? if so, put them in front of another.

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 
+10
source

Source: https://habr.com/ru/post/898282/


All Articles