【问题标题】:Rewrite virtual subdomain to subfolder and keep original url将虚拟子域重写为子文件夹并保留原始 url
【发布时间】:2012-10-13 12:51:07
【问题描述】:

我现在在网上搜索了几天,但没有找到任何答案。这就是我想做的:

我网站上的每个用户都会收到一个个人子域,例如http://username.domain.com。 当然这是一个虚拟子域。

现在,我已经设置了通配符 DNS A 记录,打开 mod_rewrite 并实现:

当有人浏览 http://anysubdomain.domain.com 时,他们会被重定向到 http://www.domain.com/sub/index.php?var=anysubdomain。在 index.php 中有一个函数可以根据 'var' 变量(子域的名称)获取唯一信息。

我想要做的是当有人浏览 http://anysubdomain.domain.com 他被重定向到 http://www.domain.com/sub/index.php?var=anysubdomain (就像现在) 但原始 URL (http://anysubdomain.domain.com) 保留在浏览器栏中。所以看起来每个用户都有自己的子域。

这是我目前使用的htaccess文件:

RewriteEngine On 
Options +FollowSymlinks 


RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} !^static\.domain\.com
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/sub/index.php?username=%2 [L]

我希望一切都清楚,如果没有:询问,我提供更多信息!

提前致谢!

尤里

【问题讨论】:

    标签: .htaccess redirect subdomain


    【解决方案1】:

    只要您的所有子域和主域共享同一个文档根目录,您就可以从规则中去掉 http://www.domain.com 位:

    RewriteCond %{HTTP_HOST} !^www\.domain\.com
    RewriteCond %{HTTP_HOST} !^static\.domain\.com
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain.com$ [NC] 
    RewriteRule ^(.*)$ /sub/index.php?username=%2 [L]
    

    否则,您需要通过添加 P 标志来使用 mod_proxy:

    RewriteCond %{HTTP_HOST} !^www\.domain\.com
    RewriteCond %{HTTP_HOST} !^static\.domain\.com
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain.com$ [NC] 
    RewriteRule ^(.*)$ http://www.domain.com/sub/index.php?username=%2 [L,P]
    

    虽然第一种方式会更有效率。

    【讨论】:

    • 我怎样才能让我的所有子域都拥有与主域相同的文档根?这是当前的结构:/domains/domain.com/htdocs/ - 主域 - 静态 因为我的子域是虚拟的(静态子域除外),它们实际上没有根(或者我错了吗?)。当我删除 .htaccess 文件中的“domain.com”时,出现 500 内部服务器错误。当我通过添加 P 标志来使用 mod_proxy 时也是如此。
    • @YuriH 很可能您的所有子域都具有相同的文档根目录,否则您需要联系您的托管服务支持。
    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2014-10-09
    • 2014-04-19
    • 2017-08-24
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多