Apache Rewrite Trailing Slash

时间:2014-01-31 06:46:21

标签: apache .htaccess redirect

我正在尝试使用以下内容:

Redirect 301 /example/cookies http://test.butterscotch.co.uk/cookies?ref=test

最终会重定向到:

http://test.butterscotch.co.uk/cookies?ref=testexample/cookies

但是,如果我这样做:

Redirect 301 /example/cookies/ http://test.butterscotch.co.uk/cookies?ref=test

我尝试了网址:

http://oldhost.com/example/cookies/ (with the trailing slash)

它正确地重定向到新网址。

我需要能够使用Redirect,因此它不必具有尾部斜杠。

1 个答案:

答案 0 :(得分:1)

不确定是什么导致了您的第一个问题,但Redirect指令可能不是您在重定向目标中使用查询字符串时所需的指令。 `Redirect指令将两个URL路径链接在一起,所以如果我有一个规则:

Redirect /abc /xyz

我转到/abc/,我显然会被重定向到/xyz/,如果我转到/abc/def/g,我会被重定向到/xyz/def/g。当我在混合中有一个查询字符串时:

Redirect /abc /xyz?i=123

然后我转到/abc/def/g,我最终会被重定向到/xyz?i123/def/g,这显然不是我想要的。

请尝试使用RedirectMatch

RedirectMatch 301 /example/cookies/?$ http://test.butterscotch.co.uk/cookies?ref=test

或使用mod_rewrite:

RewriteEngine On
RewriteRule ^example/cookies/?$ http://test.butterscotch.co.uk/cookies?ref=test [L,QSA,R=301]