我的所有
RedirectPermanent
规则都会向 URL 添加查询字符串。该不需要的查询与由 RewriteRule
重新定义的重定向粗略 URL 匹配,如下所示:
RewriteRule ^page-([0-9]+)-(.*)$ index.php?page=page&id=$1 [L]
这条规则可以很好地创建这种 URL :
https://example.com/page-320-enfants
301重定向定义如下:
RedirectPermanent /page-320-enfants /page-2028-pour-les-invitees
问题:这会向新创建的 url 添加查询字符串:
https://example.com/page-2028-pour-les-invitees?page=page&id=320
我尝试使用
RewriteRule
(旧 URL 到新 URL)而不是旧问题中推荐的 RedirectPermanent
,但它具有完全相同的效果。
我错过了什么吗?
更新:
按照建议,我尝试使用
RewriteRule
放在通用规则本身之前:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect Permanent #
RewriteRule ^page-2028-pour-les-invitees$ page-320-enfants [R=301,L]
# Common rules #
...
RewriteRule ^page-([0-9]+)-(.*)$ index.php?page=page&id=$1 [L]
...
此尝试根本不会触发。问题可能出在语法上吗?
是的,
RedirectPermanent
(mod_alias)指令将添加先前重写的查询字符串。尽管指令的表面顺序不同,但首先处理 RewriteRule
(mod_rewrite) 指令,因此在内部将请求重写为 index.php?page=page&id=320
。 RedirectPermanent
指令稍后处理,但仅查看最初请求的 URL 路径(而不是重写的 URL),因此会触发重定向,但随后会附加来自内部重写的 new 查询字符串(否则将附加原始请求)。
如果您已经使用 mod_rewrite 进行内部重写(以及可能的其他重定向),那么您需要始终使用 mod_rewrite 以避免此类冲突。
我尝试使用
(旧 URL 到新 URL)而不是旧问题中推荐的RewriteRule
,但它具有完全相同的效果。RedirectPermanent
这听起来像是您看到了来自早期
RedirectPermanent
指令的缓存响应(301 由浏览器持久缓存)。如果在上述重写之前放置了正确的RewriteRule
,它将按预期工作。如果你把它放在之后,它不会做任何事情,因为 URL 路径不匹配。
RewriteRule /page-320-enfants /page-2028-pour-les-invitees [R=301,L]
此规则似乎位于正确的位置(在内部重写之前),但是,
RewriteRule
pattern(第一个参数)不正确,并且与请求的 URL 不匹配,因此该规则不执行任何操作。
删除目录前缀(
RewriteRule
文件的文件路径)后,.htaccess
模式(正则表达式)与 URL 路径匹配。目录前缀始终以斜杠结尾,因此匹配的 URL 路径永远不会以斜杠开头。所以这条规则应该这样写:
RewriteRule ^page-320-enfants$ /page-2028-pour-les-invitees [R=301,L]
从
/page-320-enfants
重定向至 /page-2028-pour-les-invitees