我正在重新设计一个网站,并在一个临时环境中工作。域名将保持不变,但不同的页面正在移动或被新页面取代。
我需要将301重定向放在适当的位置,以确保旧页面重定向到新页面。但我正在苦苦思索最好的方法。
我使用htaccess找到了301指令的语法,并对一些urls进行了测试,如下所示:
Redirect 301 /staging/blog https://example.com/staging/
Redirect 301 /staging/research/papers https://example.com/staging/research/countries/
Redirect 301 /staging/research/papers/report https://example.com/staging/research/countries/country/activities但我发现,当我实现这一点时,第三个页面的重定向重定向到:https://example.com/staging/research/countries/activities (完全忽略counry页面)
有人能帮我解释一下为什么会这样吗?和/或解释我应该如何做?
发布于 2021-05-20 02:13:29
配置文件(".htaccess“是”分布式配置文件“)中的指令是从上到下处理的。因此,您需要将更具体的规则放在更高的位置,而将不太具体的规则放在更低的位置:
Redirect 301 /staging/blog https://example.com/staging/
Redirect 301 /staging/research/papers/report https://example.com/staging/research/countries/country/activities
Redirect 301 /staging/research/papers https://example.com/staging/research/countries/允许更多灵活性的替代方案由重写模块提供:
RewriteEngine on
RewriteRule ^/?staging/blog/?$ https://example.com/staging [R=301]
RewriteRule ^/?staging/research/papers/report/?$ https://example.com/staging/research/countries/country/activities [R=301]
RewriteRule ^/?staging/research/papers/?$ https://example.com/staging/research/countries [R=301]或者更通用的变体:
RewriteEngine on
RewriteRule ^/?staging/blog(.*)$ https://example.com/staging$1 [R=301]
RewriteRule ^/?staging/research/papers/report(.*)$ https://example.com/staging/research/countries/country/activities$1 [R=301]
RewriteRule ^/?staging/research/papers(.*)$ https://example.com/staging/research/countries$1 [R=301]你的问题,或者更好的例子,不允许有一个更准确的答案。目前还不清楚要重定向的URL是模式(前缀)还是最终URL。
特别是很难预测你是否会遇到重写循环的情况。你必须通过添加条件来解决这个问题。这就是为什么您应该更多地了解这些指令是如何工作的。你需要自己掌握这一点。这就是为什么您应该开始阅读apache http服务器带来的模块的文档。它们的质量非常好,并且有非常好的例子。
https://stackoverflow.com/questions/67604418
复制相似问题