首页 > 解决方案 > 在 RedirectRule 发生后重定向到其他网站

问题描述

我有一个网站,比方说www.example.com,但这曾经是www.example.nl. 来自的所有流量www.example.nl现在都重定向到www.example.com,但是由于我们更改了一些命名约定,www.example.nl/seeds现在必须重定向到www.example.com/nl/flower-seeds

我得到的.htaccess文件包含以下代码: RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]

Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1

当我导航到时,www.example.nl/seeds/我最终www.example.com/seeds/以 404 结尾,因为我错过了该/nl/flower-部分。

我认为当 URL 已被 RewriteRule 更改时,重定向无法正常工作。你将如何解决这个问题?任何帮助表示赞赏!

标签: .htaccess

解决方案


您应该排除 /seeds/这样的目录形式一般规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]

RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]

注意:清除浏览器缓存然后测试

另外,不要像您所做的那样使用带有重定向的正则表达式:

Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1

在这里这(.*)没有任何意义,你可以使用任何一个RedirectMatch or RewriteRule


推荐阅读