.htaccess redirect to subdomain based on query str

2019-08-23 05:15发布

问题:

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.