为特定重定向规则添加尾部斜杠 [英] adding a trailing slash for specific redirection rules
问题描述
我正在将网站迁移到 Wordpress,同时更改他们在旧平台上的复杂 URL 结构.典型的 URL 如下所示:
I'm moving a website to Wordpress and simultaneously changing the convoluted URL structure they had on their old platform. A typical URL looks something like this:
/blog/index.php/weblog/comments/post-name
但也可以是这样的:
/blog/index.php/weblog/comments/post-name/
(不是尾部斜杠的不一致使用)
(not the inconsistent use of the trailing slash)
这只是相同内容的大约十几种不同路径的一个示例.因此,我创建了一堆特定的重写规则,如下所示:
This is just one example of about a dozen different paths to the same content. As a result I've created a bunch of specific rewrite rules that look something like this:
RewriteRule ^blog/index.php/weblog/comments/(.*)$ http://host.com/$1
RewriteRule ^blog/index.php/weblog/comments/(.*)$ http://host.com/$1
问题是如果原始 URL 不包含尾部斜杠,我会遇到双 301 情况,这显然对 SEO 非常不利.重定向是这样的:
The problem is if the original URL doesn't contain a trailing slash I get a double 301 situation, which apparently is pretty bad for SEO. The redirect goes like this:
/blog/index.php/weblog/comments/post-name [301]
http://host.com/post-name [301]
http://host.com/post-name/ [200]
Wordpress 正在添加该斜线作为已设置的永久链接结构的一部分.我的问题是:我可以检查尾部斜杠并在 Wordpress 介入之前添加它吗?
Wordpress is adding that slash as part of the permalink structure that's been set up. My question is this: Can I check for a trailing slash and add it if it's missing before Wordpress gets involved?
我试过 ([^./]+)$ 映射到/$1/但是当我想删除帖子名称之前的所有内容时,这似乎捕获了评论目录.
I've tried ([^./]+)$ mapping to /$1/ but that seems to capture the comments directory when I want strip away everything before the post name.
发布前的最后一分钟:作为 mod_rewrite 和 regex 的菜鸟,我突然想到,也许一个全局重写规则可以在帖子名称和条件添加尾部斜杠之前删除所有内容,效果最好.可行吗?
LAST MINUTE THOUGHT BEFORE POSTING: Being a noob at mod_rewrite and regex it occurs to me that maybe one global rewrite rule that removes everything before the post name and conditional adds a trailing slash would work best. Workable?
推荐答案
也许这会奏效
RewriteRule ^blog/index.php/weblog/comments/([^/]+)/?$ http://host.com/$1/
这篇关于为特定重定向规则添加尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!