.htaccess - 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/contact
URL,它会给我以下页面:
http://solutionsgtr.ca/fr/deaneco/accueil.html/contact
第一条规则有效(deaneco/
到solutionsgtr.ca/fr/deaneco/accueil.html
)。
我觉得两条线都混在一起了,给了我错误的页面,那是不存在的,所以我收到 404 错误。
解决方案
这里有几个问题:
该
Redirect
指令(mod_alias 的一部分)是前缀匹配的,匹配后的所有内容都附加在目标 URL 的末尾。这解释了您看到的重定向。(
RewriteRule
mod_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/contact
URL
如果您要重定向到同一主机,则无需在目标 URL 中显式包含方案 + 主机名,因为这将是默认设置。
推荐阅读
- android - 改变小部件的方向会破坏它
- typescript - 打字稿中重载参数的命名约定
- ios - NSNotification.Name.UIKeyboardWillShow 使用 Gboard 多次调用
- android - 如何更改包含 ViewPager 的 Fragment 中的视图?
- hyperledger-fabric - 在多台计算机上设置 Fabric 网络:无法在 2nd Org 的对等点上实例化链码
- ios - 在将名称设置为段控制时,“AnyObject”不是“NSObject”的子类型
- python - Python,转换flac文件的通道
- javascript - 安装后如何从文件中删除 npm?
- php - 如何将来自url的文件作为函数中的变量传递?
- android - 我无法点击文字