在htaccess中更正多个301重定向的语法

时间:2017-03-18 17:20:29

标签: apache .htaccess redirect

我正在尝试将旧网址中的多个网页重定向到新网址。

Redirect 301 /eng/loc.php /en/location/
Redirect 301 /eng/rest.php /en/restaurant
Redirect 301 /eng/accom.php /en/accommodation-rates
Redirect 301 /eng/fish.php /en/fishing
Redirect 301 /eng/hiking.php /en/hiking-cycling

这一直有用,直到我添加这一行:

Redirect 301 /eng /en/home

添加此项会导致第一个代码块中的URL错误输出。我认为这是我的错误语法的冲突,但无法在stackoverflow中找到类似的解决方案,并且无法将我的问题应用于htaccess文件的文档。

我已经尝试将escape \和\ s添加到" / eng"在上面这一行,但那不是问题。

这里的正确语法是什么?

1 个答案:

答案 0 :(得分:0)

实际解决方案是使用RedirectMatch作为"目录"。最初的解决方案'每个网址的引号都是错误的,因为我稍后在重定向其他链接时发现。

RedirectMatch 301 ^/eng$  /en/home  
Redirect 301 "/eng/loc.php" "/en/location/"
Redirect 301 "/eng/rest.php" "/en/restaurant"
Redirect 301 "/eng/accom.php" "/en/accommodation-rates"
Redirect 301 "/eng/fish.php" "/en/fishing"
Redirect 301 "/eng/hiking.php" "/en/hiking-cycling"

这是我找到解决方案的地方:http://www.askapache.com/htaccess/301-redirect-with-mod_rewrite-or-redirectmatch/