0

Is there any way in mod-rewrite that sub directory refer to a sub domain.

so:

maindomain.com/something refer to something.maindomain.com

thanks!

4

1 回答 1

1

I share here my hard tested RewriteRule set, hope that you can adapt to your usage:

Rewrite without Redirect via /.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www).*)\.maindomain\.com$
RewriteRule ^(.*)$ http://www.maindomain.com/%1/$1 [L]

It tells apache to capture the host name that is not www in the RewriteCond backreference %1 (percentage 1). And capture the relative url in RewriteRule backreference $1 (dollar 1).

It does this:

http://east.maindomain.com/ => http://www.maindomain.com/east/
http://east.maindomain.com/asia/us/index.php => http://www.maindomain.com/east/asia/us/index.php

Rewrite with Redirect via /.htaccess (visible in address bar of browser):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www).*)\.maindomain\.com$
RewriteRule ^(.*)$ http://www.maindomain.com/%1/$1 [R,L]
于 2013-11-10T19:00:29.397 回答