.htaccess point a subdomain to a directory

2019-06-11 06:50发布

问题:

I have the following directory structure:

site.com/
- index.php
- desktop/
-- index.php
- mobile/
-- index.php

The index.php of site.com redirects to desktop/index.php or mobile/index.php, depending of user platform.

The problem is that I need different subdomains to point to respective directories:

mobile.site.com -> site.com/mobile
desktop.site.com -> site.com/desktop

My application also uses rewrite to make SEO friendly URL's, so I need:

mobile.site.com/login -> site.com/mobile/login
mobile.site.com/user/1 -> site.com/mobile/user/1
...

Any suggestion to .htaccess file?

Thanks in advance.

回答1:

RewriteCond %{HTTP_HOST} ^mobile RewriteRule ^/?(.*) /mobile/$1 [PT,L]

RewriteCond %{HTTP_HOST} ^desktop RewriteRule ^/?(.*) /desktop/$1 [PT,L]

On the other hand, if you want to redirect, rather than just pass-through, change that PT to an R in each case.



回答2:

Try this

RewriteCond %{HTTP_HOST} ^(.*).site.com$

RewriteRule (.*) %1/$1 [L]