Ben
Ben

Reputation: 11

Mod Rewrite Rule for Dynamic URL - Is this possible?

I've given myself a headache trying to figure out if this can be done. I have a forum that was recently migrated, leaving thousands of broken dynamic links.

A typical URL looks like this:

http://domain.com/Forum_Name/b10001/25/
('b10001' refers to the forum ID number and the last number refers to the page number.)

The new URL is formatted like this:

http://domain.com/forums/Forum_Name.10001/
(No page number. Also, notice the 'b' is no longer in front of the ID number.)

Is there a rewrite rule that can achieve this?

Upvotes: 1

Views: 82

Answers (2)

mgarciaisaia
mgarciaisaia

Reputation: 15560

I'm not a rewriter, but following what I've read here, something like this should work:

RewriteRule    ^([A-Za-z0-9-]+)/b([0-9])+(/[0-9]+)?/?.*$    forums/$1.$2/   [NC,L]

^([A-Za-z0-9-]+) says "begins with an alphanumeric string", then there's the /b constant, followed by [0-9]+ (one or more digits), and then an optional / with one or more digit (the page number, (/[0-9]+)?), and lastly, it ends with an optional slash (/?$).

If the URL matches that pattern, then it's rewritten to forums/$1\.$2/. \. escapes the dot (it's a wildcard), $1 is the first match of the pattern (that first alphanumeric string which is the forum name), and $2 is the second match, namely, the number after the b.

Finally, NC means pattern is case-insensitive, and L is "last" - so you don't process any other rule. I think that is most up to you, just read the linked article and pick the flags you need :)

Edit: corrected pattern checking with http://htaccess.madewithlove.be/

Upvotes: 1

AlwaysBTryin
AlwaysBTryin

Reputation: 1964

I think what you're looking for is

RewriteRule ^([a-zA-Z0-9_]+)/b([0-9]+)/.*$ forums/$1/$2/

Make sure the contents of the [] parts match the format you're using for forum names and ids.

For parameters, you probably want R=301 to force a permanent redirect.

Upvotes: 0

Related Questions