htaccess Rewrite Rule for subdomain to page

跟風遠走 提交于 2019-11-30 09:05:26

问题


Currently I have a page residing at:

http://www.example.com/site/page

In my htaccess I have the current rule:

RewriteRule ^site/(.*)$ retailer.php?linklabel=$1 [QSA]

What would the rule look like to have the URL for that page become it's own subdomain:

http://page.example.com

Thanks for any help!!

So far I have tried the following rule which hasn't worked:

RewriteRule http://(.*)$.example.com retailer.php?linklabel=$1 [QSA]

Maybe I need to do something in cpanel first regarding a wildcard subdomain? I'm not too sure...


回答1:


Try:

RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.example.com$ [NC]
RewriteRule ^$ retailer.php?linklabel=%1 [L,QSA]

You can't match against the host in a RewriteRule, so you'll need to do it against the %{HTTP_HOST} var in a RewriteCond, then use the %1 backreference to get that match.

This will currently only match against URI's that are exactly /. So it won't match against http://page.example.com/something unless you change the regular express at from ^$ to ^something$.




回答2:


Try this one :

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule .* retailer.php?linklabel=%1 [L,QSA]


来源:https://stackoverflow.com/questions/11976475/htaccess-rewrite-rule-for-subdomain-to-page

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