【问题标题】:.htaccess redirect to a single subdomain.htaccess 重定向到单个子域
【发布时间】:2011-09-25 22:05:48
【问题描述】:

我有一个类似www.example.com 的网站,我想将每个点击重定向到此 URL,并将子 URL 重定向到子域上的单个 URL。以下是一些例子

www.example.com 应该被重定向到http://test.example.com

www.example.com/show/mypage1 应该被重定向到http://test.example.com

www.example.com/show/mypage2 应该被重定向到http://test.example.com

www.example.com/show/mypage3 应该被重定向到http://test.example.com

我想用.htaccess 来做。我想要302 temporary redirect。我正在使用 Apache WebServer。

【问题讨论】:

    标签: apache .htaccess redirect http-status-code-302


    【解决方案1】:

    您使用的是 Apache 还是 IIS?

    在 Apache 中:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
    

    已编辑

    已编辑:将 http://test.example.com/$1 替换为 http://test.example.com/,现在可以按要求工作了。

    【讨论】:

    • 我正在使用 Apache,您的代码将如何重定向到 http://test.example.com 我看不到任何对它的引用?
    • @Ummar,不会的。该代码将去掉 www. 前缀,这是对一个完全不同的问题的答案。
    • 重写字符串如你所见使用正则表达式字符串,所以请求将被转发到新域
    • @TRiG,那怎么办,请告诉我?
    • 您需要将 mod_rewrite 添加到您的 Apache 服务器配置中,在使用终端的新系统上最常见的是:sudo a2enmod rewrite
    【解决方案2】:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
        RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 2010-11-22
      • 2011-08-27
      • 1970-01-01
      相关资源
      最近更新 更多