首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >301重定向正在删除部分URL

问301重定向正在删除部分URL
EN

Stack Overflow用户
提问于 2021-05-19 21:39:28
回答 1查看 15关注 0票数 0

我正在重新设计一个网站,并在一个临时环境中工作。域名将保持不变,但不同的页面正在移动或被新页面取代。

我需要将301重定向放在适当的位置,以确保旧页面重定向到新页面。但我正在苦苦思索最好的方法。

我使用htaccess找到了301指令的语法,并对一些urls进行了测试,如下所示:

代码语言:javascript
复制
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页面)

有人能帮我解释一下为什么会这样吗?和/或解释我应该如何做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-20 02:13:29

配置文件(".htaccess“是”分布式配置文件“)中的指令是从上到下处理的。因此,您需要将更具体的规则放在更高的位置,而将不太具体的规则放在更低的位置:

代码语言:javascript
复制
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/

允许更多灵活性的替代方案由重写模块提供:

代码语言:javascript
复制
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]

或者更通用的变体:

代码语言:javascript
复制
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服务器带来的模块的文档。它们的质量非常好,并且有非常好的例子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67604418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档