如何进行301重定向以删除URL中包含单词“comment”和随机数字部分的内容

huangapple go评论68阅读模式
英文:

How to 301 redirect removing a section of URL containing the word comment + random number

问题

我最近将我的网站迁移到一个新域名,并做了一些改动。在这个过程中,似乎由于像这样的URL而生成了大量的404错误:

mywebsite.com/theblogpost/comment-68875

这在许多博客文章中都会发生,所以博客文章部分是可变的,末尾的数字也是可变的。
我尝试使用一个正则表达式测试工具和一个htaccess测试工具进行实验,但似乎我不够聪明来解决这个问题。

我大约达到了\b(\wcomment-\w)\b,这与正则表达式测试中的短语匹配,但我无法弄清楚如何正确应用它到RewriteCond/RewriteRule。

提前感谢任何帮助!

英文:

I recently moved my website to a new domain and changed a bunch of things. In the process, I seem to have ended up generating a lot of 404 errors due to URLs like this:

mywebsite.com/theblogpost/comment-68875

This occurs on a lot of blog posts, so the blog post bit is variable as is the number at the end.
I tried experimenting with a RedEx testing tool and a htaccess tester, but it would seem that I am not smart enough to work it out

I got about as far as \b(\wcomment-\w)\b which matched the phrase in RegEx testing but couldnt work out how to correctly apply that to a RewriteCond/RewriteRule

Thanks in advance for any help!

答案1

得分: 0

以下是翻译好的部分:

要将所有这种请求重定向到相同的目标:

RewriteEngine on
RewriteRule ^/?[^/]+/comment-\d+/?$ / [R=301,END]

要将所有这种请求重定向到实际的博客文章:

RewriteEngine on
RewriteRule ^/?([^/]+)/comment-\d+/?$ /$1 [R=301,END]

我在这里使用了绝对路径,因为问题似乎表明您正在使用DOCUMENT_ROOT文件夹作为基础。显然,也可以使用相对路径来实现这些规则。

我个人明确建议在您的HTTP服务器的中央主机配置中实施这些规则。如果您使用Apache HTTP服务器并且事先启用了此功能(请参阅AllowOverride指令的文档),则可以使用分布式配置文件。但这会增加复杂性并严重降低HTTP服务器的速度。因此,只有在无法访问中央配置(即,如果您使用的是廉价的托管提供程序)时才使用这种方法。

英文:

The question is a bit vague, but I think the important detail is how to match such incoming requests. You have to adjust the rest yourself ...

To redirect all such requests to the same target:

RewriteEngine on
RewriteRule ^/?[^/]+/comment-\d+/?$ / [R=301,END]

To redirect all such requests to the actual blog post:

RewriteEngine on
RewriteRule ^/?([^/]+)/comment-\d+/?$ /$1 [R=301,END]

I used absolute path here since the question reads as if you are using the DOCUMENT_ROOT folder as base. Obviously the rules can also be implemented using relative path.

I personally clearly advise to implement such rules in the central host configuration of your http server. You can instead use a distributed configuration file if you are using an apache http server and if you have enabled that featture beforehand (see documentation of the AllowOverride directive). But that adds complexity and really slows down the http server. So only do that if you have no access to the central configuration (read: if you are using a cheap hosting provider).

huangapple
  • 本文由 发表于 2023年3月23日 12:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定