views:

431

answers:

1

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  A: 

Perhaps this will work

RewriteRule ^blog/index.php/weblog/comments/([^/]+)/?$ http://host.com/$1/
Steef
That seems to be doing the trick! Thanks a bunch, I've been banging my head against all the global solutions out there and not getting anything to work.
rustbucket
As with all things of this nature I've run into a snag. Some of the original URL's have index.php at the end of them (post-name/index.php). If it's not too hard, I'd love it if someone could help revising Steef's rule to include that possibility. Sorry, I suck at regex.
rustbucket