首页 > 解决方案 > HTACCESS 301 重定向不断发送到错误的页面

问题描述

我正在尝试将旧页面从我重新设计的网站重定向到新页面,但它不起作用。

.htaccess这是我在文件中关于该域的两行代码:

Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]

如果继续solutionsgtr.ca/deaneco/contactURL,它会给我以下页面:

http://solutionsgtr.ca/fr/deaneco/accueil.html/contact

第一条规则有效(deaneco/solutionsgtr.ca/fr/deaneco/accueil.html)。

我觉得两条线都混在一起了,给了我错误的页面,那是不存在的,所以我收到 404 错误。

标签: .htaccessredirectmod-rewritemod-alias

解决方案


这里有几个问题:

  • Redirect指令(mod_alias 的一部分)是前缀匹配的,匹配后的所有内容都附加在目标 URL 的末尾。这解释了您看到的重定向。

  • ( RewriteRulemod_rewrite)模式 ^/deaneco/contact永远不会在.htaccess上下文中匹配,因为匹配的 URL 路径不以斜杠开头。所以,这条规则目前没有做任何事情。

您应该避免混合来自两个模块的重定向,因为它们在请求期间独立且在不同时间执行(mod_rewrite 首先执行,尽管指令的顺序很明显)。

要么使用 mod_alias,首先对最具体的指令进行排序:

Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html

注意:您需要清除浏览器缓存,因为错误的 301(永久)重定向将被浏览器缓存。使用 302(临时)重定向进行测试,以避免潜在的缓存问题。

或者,如果您已经将 mod_rewrite 用于其他重定向/重写,则考虑使用 mod_rewrite 代替(以避免如上所述的潜在冲突):

RewriteEngine On

RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]

QSA标志不是必需的,因为默认情况下将查询字符串传递给替换

在这种情况下,指令的顺序RewriteRule并不重要,因为它们只匹配特定的 URL。


如果继续solutionsgtr.ca/deaneco/contactURL

如果您要重定向到同一主机,则无需在目标 URL 中显式包含方案 + 主机名,因为这将是默认设置。


推荐阅读