【问题标题】:redirect using htaccess from subdomain to domain (individual files)使用 htaccess 从子域重定向到域(单个文件)
【发布时间】:2012-04-21 16:50:03
【问题描述】:

我有一个子域 (store.example.com)。我不仅想重定向来自 store.example.com 的流量,还想将其重定向到域上的特定文件(我能找到的一切都是基于重定向整个子域)

所以我想要 store.example.com/Science-Fiction -> www.bundoranpress.com/category/1/Science-Fiction

如何使用 htaccess 文件编写此内容?

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您将需要几个 RewriteConds 或(在我看来更好的方法)一个处理您的请求的 php 脚本。对于第一种方法:

    RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish) [NC]
    RewriteRule (.*) http://www.bundoranpress.com/$1/$2 [L,R]
    

    这会将 store.example.com/ThanksForAllTheFish 重定向到销售 Douglas Adam 书籍的 www.bundoranpress.com/store/ThanksForAllTheFish。 现在,您可以像这样在第二个 RewriteCond 中添加所有可能的术语:

    RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish|Universe|Hitchhiking) [NC]
    

    这将捕获其中一个术语并重定向到相应的地址(例如 www.bundoranpress.com/store/Universe)。你会发现这很快就会变得很困难。 更好的方法是将 all url 从特定文件夹和子域重定向到处理其余部分的 php 脚本,如下所示:

    RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} ^books/(.*) [NC]
    RewriteRule (.*) http://www.bundoranpress.com/index.php?sub=$1&cat=$2 [L,R]
    

    这会将 store.example.com/books/ThanksForAllTheFish 重定向到 www.bundoranpress.com/index.php?sub=store&cat=ThanksForAllTheFish。您可以分别通过 $_GET["sub"] 和 $_GET["cat"] 访问变量。这为您提供了更大的灵活性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 2010-11-22
      • 2023-03-17
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2014-08-19
      相关资源
      最近更新 更多