Rewrite subdomain

偶尔善良 提交于 2019-12-23 01:58:09

问题


Obviously a pretty simple question: how do I rewrite

(value1).example.com/(value2)

to

/(value1)/(value2)

I've tried a couple of things without success.

Thank you in advance.


回答1:


Try this rule:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond $1/%1 !^([^/]+)/\1$
RewriteRule ^/([^/]+)? /%1%{REQUEST_URI} [L]

Some explanation: My previous suggestion caused an infinite recursion as the L flag causes a restart of the rewriting process with the new URL:

Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

The second condition will avoid this as it compares the subdomain part with the first path segment. And only if they are different the condition is true and the rewrite takes place.



来源:https://stackoverflow.com/questions/3897455/rewrite-subdomain

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