指着用的.htaccess一个子域到子文件夹个子、文件夹、htaccess、域到子

2023-09-02 00:29:54 作者:毁誉由人

我的虚拟主机提供商自动转发所有的请求* .mydomain.com到顶层域mydomain.com。

My webhost automatically forwards all requests to *.mydomain.com to the toplevel domain mydomain.com.

我想映射任何子域到一个特定的文件夹在我的顶级域名。即sub.example.com必须映射到example.com/someFolder(没有在地址栏的变化)。

I wanted to map any subdomain to a specific folder on my toplevel domain. i.e. sub.example.com must be mapped to example.com/someFolder (without change in the address bar).

在网络上周围挖后,我想出了这一点:

After digging around on the net, I came up with this:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?[^.]+.example.com.*$
RewriteRule (.*) http://example.com/myfolder/$1 [L]

这似乎运作良好,但有一个问题:当我去到URL sub.example.com,在地址栏上的网址example.com/myfolder~~V。但是,当我做这样的事情sub.example.com/login - 这映射到example.com/sub/login~~V不正确的地址栏中的变化。任何帮助非常AP preciated!

This seems to work well, except for one problem: When I go to the URL sub.example.com, the URL in the address bar changes to example.com/myfolder . But, when I do something like sub.example.com/login - this maps to "example.com/sub/login" properly without the change in the address bar. Any help greatly appreciated!

推荐答案

只需要小的变化:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?[^.]+.example.com.*$
RewriteRule (.*) myfolder/$1 [L]

剥离出来的http://在此告诉Apache发送一个重定向头,而不是正确的服务器端重写规则

stripped out http:// at the rule which tells Apache to send a Redirect header instead of proper server-side rewrite.