3

我知道这是一个常见的问题/问题,但我花了几个小时寻找答案,我发现太多不同的解决方案彼此差异太大。我想知道实现这一目标的“最佳”方式(最佳实践)。

我的 htacces 文件如下所示:

RewriteEngine On

# Redirect non-www URLs to www-prefixed ones
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Redirect HTTPS requests to HTTP ones
RewriteCond %{HTTPS} =on
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# Redirect URLs with trailing slashes to ones without it
RewriteRule ^([^/\.]+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

# Rewrite pretty URLs
RewriteRule ^([^/\.]+)$ index.php?page=$1 [L]

现在需要最后一个重要步骤:重定向旧 URL,例如

http://www.domain.com/index.php?page=abchttp://www.domain.com/?page=abc

给新的:

http://www.domain.com/abc

我知道我可能应该检查{QUERY_STRING}并将此重定向放在重写 URL 的最后一行之前,或者可能不?如何正确编写这样的规则?

4

1 回答 1

3

要重定向http://www.domain.com/index.php?page=abc and http://www.domain.com/?page=abc到新的,请使用此附加规则:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+) [NC]
RewriteRule ^ /%1? [R=301,L]
于 2013-02-21T12:18:35.357 回答