Home > Software design >  rewriterule moved permanently from an old forum to new forum
rewriterule moved permanently from an old forum to new forum

Time:12-04

Is there a way to put a small rewriterule in .htaccess to redirect over 20.000 links from an old phpBB based forum to a new phpBB forum ? The old link is like this :

https://example.com/t123456-some-title

The new link is like this :

https://example.com/forum/viewtopic.php?t=123456

t-123456 is a variable number for topic number in the old forum, and the number is the same but with viewtopic.php?t=123456

I think that is possible with some regex, but I don't know how to make this.

Maybe something like :

RewriteEngine On

RewriteBase /

RewriteRule ^t-([0-9] )-([a-zA-Z0-9-] )$ /forum/viewtopic.php?t=$2 [R,L]

CodePudding user response:

RewriteRule ^t-([0-9] )-([a-zA-Z0-9-] )$ /forum/viewtopic.php?t=$2 [R,L]

Almost, except that the $2 backreference in the substitution string (referencing the 2nd capturing subgroup) would contain some-title, not 123456.

You don't seem to have a need to capture the title, so you can remove the surrounding parenthesis from the second group. In fact, if you are discarding the title altogether then you perhaps don't need to match this at all, unless it would result in a conflict with other URLs? You are only interested in the id.

This should ultimately be a 301 (permanent) redirect, however, it is currently a 302 (temporary) redirect (the default). So change R to R=301 once you have tested that it works as expected.

For example:

RewriteRule ^t-(\d ) /forum/viewtopic.php?t=$1 [R=301,L]

This matches any URL that starts /t-<number> and redirects to /forum/viewtopic.php?t=<number>

UPDATE: Sorry, I was mistaken, the old URL is like t123456 without the hyphen

Then just remove the hyphen in the RewriteRule pattern. ie. ^t(\d )

CodePudding user response:

Big thank MrWhite, the rewriterule work perfectly

RewriteRule ^t(\d ) /forum/viewtopic.php?t=$1 [R=301,L]

I'll make some rewriterules for other link based on your tip =)

  • Related