.htaccess redirect to subdomain based on query string parameter

大憨熊 提交于 2019-12-11 19:21:53

问题


I need to set up a redirection to sub domain based on query string.

Example: http://example.com/message.php?id=subdomain to http://subdomain.example.com/message.php

Is this possible in .htaccess?


回答1:


This will redirect all the URLs with QUERY_STRING like id=subdomain and file message.php to the appropriate subdomain:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule message.php http://%1.example.com/message.php? [R=301,L]

Mind the question mark at the end of message.php? - it is used to suppress appending the old QUERY_STRING. I added the domain name check to avoid the possibility of eternal redirect loop.



来源:https://stackoverflow.com/questions/17907596/htaccess-redirect-to-subdomain-based-on-query-string-parameter

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