首页 > 解决方案 > 301 重定向根目录而不将新根目录级联到所有其他 301 重定向

问题描述

我已成功将站点的根目录重定向到另一个站点的子目录:

#Redirect Root to subdirectory on example.com
RewriteEngine On
RewriteRule ^$ http://example.com/subdirectory [L]

我的所有其他重定向都遇到了麻烦。新根目录 (/subdirectory) 的子目录被插入到每个路径之前的重定向中,这导致重定向中断。

例子:

Redirect 301 /contact-us/ https://www.example.com/company/contact.shtml 

试图指向

https://www.example.com/subdirectorycompany/contact.shtml 

简而言之,我可以让 root 工作,或者我可以让重定向工作——但不能同时工作。任何人都可以提供一些见解吗?

我已经阅读了很多帖子,这就是我所取得的成就,但我可以使用一些帮助,谢谢。

标签: .htaccessredirect

解决方案


不要混合Redirect指令,RewriteRule因为它们来自不同的 Apache 模块并且在不同的时间被调用。

最好有这样的所有规则:

RewriteEngine On

# all specific 301 redirects go here
RewriteRule ^contact-us/?$ https://example.com/company/contact.shtml [L,NC,R=301]

#Redirect Root to subdirectory on example.com
RewriteRule ^$ http://example.com/subdirectory [L,R=301]

推荐阅读