【问题标题】:Mapping a secondary domain to a subdirectory using mod_rewrite使用 mod_rewrite 将辅助域映射到子目录
【发布时间】:2011-06-16 04:48:42
【问题描述】:

我希望将辅助域映射到文档根目录上的子文件夹。

例如,如果对域 www.example.com 的请求映射到我的 DocumentToot,则对 www.exampletwo.com 的请求转到 /sites/files/

我无法完成从 www.exampletwo.com/index.htmlwww.exampletwo.com/sites/files/index.html 的重定向,同时使 URL 仍显示 www.exampletwo.com/index.html。有什么想法吗?

【问题讨论】:

    标签: mod-rewrite apache2 dns subdirectory


    【解决方案1】:

    我相信您正在寻找这样的东西:

    RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
    RewriteRule ^/(.*) /sites/files/$1 [L]
    

    【讨论】:

    • 嗯,还是没有骰子。我尝试了上述方法并通过访问 exampletwo.com/ 对其进行了测试,但我得到 404NotFound 因为索引页面位于 /sites/files/index.html 中,并且它没有重写
    • 啊,您没有指定 www 是可选的。更新后的正则表达式应该也可以捕获。
    • 确保重写引擎开启,否则指令被忽略:RewriteEngine onRewriteEngine Directive
    【解决方案2】:
    RewriteCond %{HTTP_HOST}   ^(www\.)?example\.com [NC]
    RewriteRule ^/(.*)         http://www.exampletwo.com/$1 [L,R]
    
    RewriteCond %{HTTP_HOST}   ^(www\.)?exampletwo\.com [NC]
    RewriteRule ^/(.*)         http://www.exampletwo.com/sites/files/$1 [L,P]
    

    P 标志使用代理模块,因此客户端上的 url 不会更改(无重定向)。

    【讨论】:

    • 谢谢,但这似乎不像我预期的那样。澄清一下,当有人访问 example.com/ 时,我会将他们重定向到 exampletwo.com/sites/files/,但 mod_rewrite 会重写 URL 以仅显示 exampletwo.com/。
    • 已更改。首先你需要将example重定向到exampletwo,然后你需要屏蔽/site/files。
    猜你喜欢
    • 2015-10-26
    • 2017-07-07
    • 1970-01-01
    • 2011-05-30
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多