【问题标题】:Redirecting multiple directories in subdomain to normal domain将子域中的多个目录重定向到普通域
【发布时间】:2013-01-08 12:25:50
【问题描述】:

我正在尝试使用 htaccess 执行此操作,但它无法正常工作。

比如我需要重定向:

http://sub.domain.com/category/http://www.domain.com/sub/category/ http://sub.domain.com/category/movies/ 到http://www.domain.com/sub/listings/movies/ http://sub.domain.com/category/movies/horror/ 到http://www.domain.com/sub/listings/movies/horror/ http://sub.domain.com/events/ 到http://www.domain.com/sub/local/events/

这是我试图做的,但其中一些像 /category/ 正在捕捉所有这些。我想只用一个斜杠而不是斜杠来精确匹配,就是这样。不是子文件夹或文件。我将 htaccess 文件放在 sub.domain.com 根目录中。

Redirect 301 /category/ http://www.domain.com/sub/category/
Redirect 301 /category/movies/ http://www.domain.com/sub/listings/movies/
Redirect 301 /category/movies/horror/ http://www.domain.com/sub/listings/movies/horror/
Redirect 301 /events/ http://www.domain.com/sub/local/events/

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:

    我发现使用 mod_rewrite 指令更容易,如下所示:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
    RewriteCond %{REQUEST_URI}   ^/category/?$   [NC]
    RewriteRule .*   http://www.domain.com/sub/category/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
    RewriteCond %{REQUEST_URI}   ^/category/movies/?$   [NC]
    RewriteRule .*    http://www.domain.com/sub/listings/movies/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
    RewriteCond %{REQUEST_URI}   ^/category/movies/horror/?$   [NC]
    RewriteRule .*    http://www.domain.com/sub/listings/movies/horror/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
    RewriteCond %{REQUEST_URI}   ^/events/?$   [NC]
    RewriteRule .*    http://www.domain.com/sub/local/events/ [R=301,L]
    

    根据:

    I want to do exact match with just a trailing slash and no slash and that's it. Not sub folders or files.

    它将永久重定向您问题中描述的 URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2016-09-24
      • 2012-12-09
      • 2011-06-28
      • 1970-01-01
      相关资源
      最近更新 更多