【问题标题】:.htaccess Rewrite Based on Existence of Path in URL.htaccess 基于 URL 中是否存在路径重写
【发布时间】:2011-06-06 21:58:48
【问题描述】:

以下是场景,我有一个网站,它曾经是一个静态 HTML 网站和一个使用子域 (http://blog.domain.com) 的 WordPress 博客。

我最近将所有内容合并到一个 WordPress 安装中。为了维护旧链接,我不得不将“http://blog.domain.com/index.php/2010/10/16/post-name”之类的请求重写为“http://domain.com/index.php/2010” /10/16/post-name”。我的问题是,当我试图访问“http://blog.domain.com”时,当我希望它转到“http://domain.com/”时,我会被重定向到“http://domain.com” index.php/blog”。

所以,如果用户请求“http://blog.domain.com”(本身,带或不带斜杠),我希望它转到“http://domain.com/index.php/blog ”。如果他们请求“http://blog.domain.com/some-link-to-a-post”的旧 URL,我希望它重定向到“http://domain.com/some-link-to-一个帖子”。换句话说,如果它是实际帖子的 URL,我只想去掉“博客”子域。如果它是指向博客主页面的旧链接,我想删除“博客”子域并附加“/index.php/blog”

http://blog.domain.com/ -> http://domain.com/index.php/blog

http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/ 2010/10/16/post-title

希望这很清楚。我不是 htaccess 专家,所以希望有人可以在这里帮助我。提前致谢!

【问题讨论】:

    标签: .htaccess mod-rewrite blogs subdomain


    【解决方案1】:

    在重写结束时使用[L] 命令将告诉 htaccess 这是它应该匹配的最后一条规则。如果您将一条规则与您的第一个条件匹配在顶部,然后您说您已经在它之后创建了另一个重写规则,您应该会得到预期的结果。

    试试这个:

    RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
    # Your other rewrite here #
    

    【讨论】:

      【解决方案2】:

      我无法让该解决方案发挥作用。但是,我使用了以下内容:

      RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
      RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
      

      这会以http://domain.com/index.php/blog/index.php/2010/06/04/post-title 之类的 URL 结尾,但 Wordpress 足够聪明,可以修复它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 2013-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        相关资源
        最近更新 更多