Rewrite all virtual subdomains to same folder using htaccess

假装没事ソ 提交于 2019-12-21 06:05:25

问题


I have been searching the internet trying to find a solution to this problem but cannot find a definite solution.

I am trying to redirect rewrite

anything.domain.com/anypage.php?sub=anything&including=get_parameters

to

domain.com/users/anypage.php?sub=anything&including=get_parameters


I have found several pages with possible solutions but they all differ slightly to my needs, and editing them continually ends in failure.

Any help would be much appreciated. Thank you.

P.S Wildcard DNS are enabled and everything is set up correctly in apache.

Edit:

I ended up using the folowing, it seems to work pretty well. Thanks.

RewriteCond %{HTTP_HOST}
^(.*).domain.com RewriteCond
%{HTTP_HOST} !^www.domain.com [NC]
RewriteRule ^(.*)$
http://domain.com/users/$1?sub=%1 [P]

回答1:


Try this rule in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

# redirect for http
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

# redirect for https
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters

$1 is your REQUEST_URI


来源:https://stackoverflow.com/questions/5657791/rewrite-all-virtual-subdomains-to-same-folder-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!