Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

mod rewrite - adding a trailing slash for specific redirection rules

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps this will work

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...