使用 .htaccess 将子域指向子文件夹 [英] Pointing a subdomain to a subfolder using .htaccess

查看:28
本文介绍了使用 .htaccess 将子域指向子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的虚拟主机自动将所有对 *.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 时,地址栏中的 URL 更改为 example.com/myfolder .但是,当我执行 sub.example.com/login 之类的操作时 - 这会正确映射到example.com/sub/login",而地址栏中没有更改.非常感谢任何帮助!

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]

在告诉 Apache 发送重定向标头而不是正确的服务器端重写的规则中去掉 http://.

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

这篇关于使用 .htaccess 将子域指向子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆