URL/Subdomain rewrite (htaccess)

给你一囗甜甜゛ 提交于 2021-02-08 07:28:30

问题


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

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