Mod rewrite 为特定重定向规则添加尾部斜杠

Mod rewrite 为特定重定向规则添加尾部斜杠,mod-rewrite,redirect,Mod Rewrite,Redirect,我正在将一个网站迁移到Wordpress,同时改变他们在旧平台上复杂的URL结构。典型的URL如下所示: /blog/index.php/weblog/comments/post-name /blog/index.php/weblog/comments/post-name/ /blog/index.php/weblog/comments/post-name [301] /blog/index.php/weblog/comments/post-name 但也可能是这样的: /blog/index.

我正在将一个网站迁移到Wordpress,同时改变他们在旧平台上复杂的URL结构。典型的URL如下所示:

/blog/index.php/weblog/comments/post-name /blog/index.php/weblog/comments/post-name/ /blog/index.php/weblog/comments/post-name [301] /blog/index.php/weblog/comments/post-name 但也可能是这样的:

/blog/index.php/weblog/comments/post-name /blog/index.php/weblog/comments/post-name/ /blog/index.php/weblog/comments/post-name [301] /blog/index.php/weblog/comments/post-name/ (不是前后斜杠的使用不一致)

这只是一个例子,其中有十几条不同的路径指向相同的内容。因此,我创建了一系列特定的重写规则,如下所示:

/blog/index.php/weblog/comments/post-name /blog/index.php/weblog/comments/post-name/ /blog/index.php/weblog/comments/post-name [301] 重写规则^blog/index.php/weblog/comments/(.*)$$1

问题是,如果原始URL不包含尾随斜杠,则会出现双301情况,这显然对SEO非常不利。重定向如下所示:

/blog/index.php/weblog/comments/post-name /blog/index.php/weblog/comments/post-name/ /blog/index.php/weblog/comments/post-name [301] /blog/index.php/weblog/comments/post-name[301] http://host.com/post-name [301] http://host.com/post-name/ [200] Wordpress正在添加斜杠作为已设置的永久链接结构的一部分。我的问题是:在Wordpress介入之前,我是否可以检查尾部斜杠并添加它(如果缺少)

我已经尝试了([^./]+)$映射到/$1/,但是当我想去掉帖子名称之前的所有内容时,它似乎捕获了comments目录

发布前的最后一分钟思考: 作为mod_rewrite和regex的一个noob,我突然想到,也许一个全局重写规则可以最好地删除帖子名称之前的所有内容,并在后面添加一个条件斜杠。可行吗?

也许这会奏效

RewriteRule ^blog/index.php/weblog/comments/([^/]+)/?$ http://host.com/$1/

这似乎是在耍花招!非常感谢,我一直在与所有的全球解决方案进行激烈的斗争,但没有取得任何效果。就像所有这些性质的事情一样,我遇到了障碍。一些原始URL的末尾有index.php(post name/index.php)。如果不太难的话,我希望有人能帮助修改Steef的规则,将这种可能性包括在内。对不起,我的正则表达式很烂。