将子域中的多个目录重定向到普通域

时间:2013-01-08 03:36:56

标签: apache .htaccess redirect

我试图用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/

1 个答案:

答案 0 :(得分:2)

我发现使用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.

它将按照您的问题

中的描述永久重定向网址