【问题标题】:How to .htaccess redirect domain to subdomain while keeping the main domain intact如何 .htaccess 将域重定向到子域,同时保持主域不变
【发布时间】:2013-04-22 21:38:03
【问题描述】:

我想重定向我域下的所有网址

domain.com/urls、www.domain.com/profile/user1.html 等到 subdomain.domain.com/urls 和 subdomain.domain.com/profile/user1.html 等

但我不想将 domain.com 和 www.domain.com 重定向到 subdomain.domain.com

是否可以通过 htaccess 实现?

编辑:我只想重定向内部页面和文件。但保持主要 domain.com 不变。

更多示例

domain.com/page1.html to subdomain.domain.com/page1.html

www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html

www.domain.com to www.domain.com (no redirection here)

【问题讨论】:

    标签: .htaccess redirect dns subdomain


    【解决方案1】:

    试试:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/.+
    RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
    RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
    RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
    

    如果您想永久重定向,请将方括号更改为:[L,R=301]

    【讨论】:

      【解决方案2】:

      您可以首先通过重写来处理主页,然后使用第二条规则重定向其他所有内容

      RewriteRule ^$ - [L]
      RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
      RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
      

      当一切正常时,您可以将R 更改为R=301

      【讨论】:

      • 这适用于子域,但主页没有加载,chrome浏览器说存在某种无限循环。
      • @user1675934 奇怪,我在测试环境中使用了这些规则,它们运行良好。无论如何,如果乔恩的回答解决了你的问题,那很好,你应该接受它。
      猜你喜欢
      • 2011-05-25
      • 2013-09-26
      • 2016-03-05
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2011-08-27
      相关资源
      最近更新 更多