301 htaccess中的重定向将旧文件名附加到新网址

时间:2013-04-23 22:32:19

标签: apache .htaccess http-redirect

我在htaccess文件中使用301重定向规则将一些旧网址重定向到新网址。但是,在一些重定向上,旧的文件名将附加到新的URL上,因此我在这些页面上出现404页面错误。

例如,这是我的重写规则

Redirect 301 /mtrx/motion-control/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer

但显示的页面是

http://www.zerolinegolf.com/golf-mtrx-for-the-golfermotion-control

这是整个.htaccess代码

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

#REDIRECTS

Redirect 301 /videos/ http://www.zerolinegolf.com/videos-2
Redirect 301 /press-releases-2/ http://www.zerolinegolf.com/media
Redirect 301 /mtrx/motion-control/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
Redirect 301 /mtrx/pelvis-power/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
Redirect 301 /mtrx/mtrx-score/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
Redirect 301 /mtrx/recommended-drills/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
Redirect 301 /mtrx/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

看起来mod_alias和mod_rewrite可能会相互干扰,也可能本身就是干扰。

请记住,Redirect指令将两个路径节点链接在一起。所以,如果你有,请说:

Redirect 301 /mtrx/ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer

该声明还需要:/mtrx/foo/bar/并将其重定向到http://www.zerolinegolf.com/golf-mtrx-for-the-golferfoo/bar/

您应该切换为使用RedirectMatchRewriteRule

对于前者:

RedirectMatch 301 ^/videos/?$ http://www.zerolinegolf.com/videos-2
RedirectMatch 301 ^/press-releases-2/?$ http://www.zerolinegolf.com/media
RedirectMatch 301 ^/mtrx/motion-control/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
RedirectMatch 301 ^/mtrx/pelvis-power/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
RedirectMatch 301 ^/mtrx/mtrx-score/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
RedirectMatch 301 ^/mtrx/recommended-drills/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer
RedirectMatch 301 ^/mtrx/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer

对于mod_rewrite,必须在wordpress规则之前放置这些规则

RewriteRule ^/?videos/?$ http://www.zerolinegolf.com/videos-2 [L,R=301]
RewriteRule ^/?press-releases-2/?$ http://www.zerolinegolf.com/media [L,R=301]
RewriteRule ^/?mtrx/motion-control/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer [L,R=301]
RewriteRule ^/?mtrx/pelvis-power/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer [L,R=301]
RewriteRule ^/?mtrx/mtrx-score/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer [L,R=301]
RewriteRule ^/?mtrx/recommended-drills/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer [L,R=301]
RewriteRule ^/?mtrx/?$ http://www.zerolinegolf.com/golf-mtrx-for-the-golfer [L,R=301]