0

我正在尝试使用 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/
4

1 回答 1

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.

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

于 2013-01-08T04:16:11.797 回答