问题
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