adding a trailing slash for specific redirection rules

寵の児 提交于 2020-01-17 03:34:07

问题


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

But could also look like this:

/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

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 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?

I've tried ([^./]+)$ mapping to /$1/ but that seems to capture the comments directory when I want strip away everything before the post name.

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?


回答1:


Perhaps this will work

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


来源:https://stackoverflow.com/questions/1077206/adding-a-trailing-slash-for-specific-redirection-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!