问题
My application dynamically creates sub domains.
If just the subdomain is used (no other path): mydomain.mysite.com
I need the subdomain to remap/rewrite (keep the same URL so its hidden from the user) to the following url mydomain.mysite.com/event/mydomain
In the instance where there are other parameters passed then I want to ignore the rewrite rule. (mydomain.mysite.com/something/else
)
I'm having trouble with the following endind up in a redirect loop
RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^(.*)$ http:// %1.mysite.com/events/%1 [L,NC,QSA]
Its also rewriting when I pass other params!
回答1:
If you have http://
as part of your rule's target, then it will automatically redirect. You want it to internally rewrite so remove the http://
and hostname. You also want to match against the URI /
because anything rlese you want to ignore the rule:
RewriteEngine on
# you may want this condition, to ignore "www"
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^/?$ /events/%1 [L]
The first condition was added to ignore hosts that start with www
, so that http://www.mysite.com/
won't get rewritten. If you don't care about that then just remove the condition. You also don't need the QSA
flag because that's implicit when you don't have a ?
in your target.
来源:https://stackoverflow.com/questions/13616669/url-subdomain-rewrite-htaccess