htaccess - Rewriting requests for subdomain to a folder, with query string
I'm trying to get a subdomain rewrite working using an Apache htaccess file and need some help please.
I am trying to get requests for http://xyz.example.com internally handled as if the user had requested http://example.com/xyz so the user does not see this URL in their 开发者_JS百科address bar - they see the subdomain version.
This is slightly complicated by the fact that anything following this also needs to be passed too, for example if the user requests http://xyz.example.com/test/word/?x=123 then this needs to be handled by the server as if they had visited http://example.com/xyz/test/word/?x=123.
The site is running on the Code Igniter framework, which had its own htaccess rules too, but I guess this rewrite would be done first before those kick in.
Any help would be greatly appreciated!
The following mod_rewrite rule set should perform the rewriting you need:
RewriteCond $0 !^(index\.php/)?xyz
RewriteCond %{HTTP_HOST} ^xyz\.
RewriteRule ^.*$ /xyz/$0 [L]
The query string gets passed along automatically, since we haven't changed it in the rewrite.
For CodeIgnitor to pick this change up though, you'll need to be using PATH_INFO
as the source of the routing information. Your $config['uri_protocol']
needs to be set to PATH_INFO
or AUTO
for this to be the case, and your CodeIgnitor RewriteRule
should look something like the following:
RewriteRule ^.*$ /index.php/$0
If you run into trouble trying to use PATH_INFO
, you'll need to find a way to get CodeIgnitor to parse $_SERVER['REDIRECT_URL']
(and add the L
flag to the first RewriteRule
up there). Hopefully this shouldn't be necessary, but if you can't get it to work otherwise I'll look into how you might do that.
精彩评论