【问题标题】:Rewrite subdomain to main domain without redirect with htaccess将子域重写为主域而不使用 htaccess 重定向
【发布时间】:2016-01-17 23:11:36
【问题描述】:

您好,我想将 sub.domain.com 重写为 domain.com/sub。
但也需要将 sub2.domain.com 重写为 domain.com/sub2 和 sub3 等...
网址不允许更改,因此不能选择重定向。
我当前的代码是无限重定向:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^(.*?)/?$ http://sub.domain.com/$1 [L]

如何更改 .htaccess 以解决此问题。

【问题讨论】:

    标签: apache .htaccess mod-rewrite rewrite subdomain


    【解决方案1】:

    循环发生是因为您重写为sub.domain.com,然后由于sub. 的存在而触发 cond 并再次重写。

    如果您不想更改地址,可以尝试使用代理标志。确保 mod_proxy 已启用:

    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
    RewriteRule ^(.*)/?$ http://domain.com/%1/$1 [P]
    

    【讨论】:

    • 非常感谢。我现在为什么会发生循环,但我不知道如何在不重定向的情况下解决它。代理标志做到了:)
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 2014-05-10
    • 2015-05-09
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多