How do I redirect dynamic URLs with parameters?

878 views Asked by At

The old URLs are like this: http://mywebsite.com/index.php?mode=thread&id=284607

The new URLs would be: http://mywebsite.com/threads/284607

As you can see, I want to grab the ID # from the old dynamic URLs and point them to the new pretty URLs.

I've tried finding the solution here and elsewhere, but keep having problems removing the "mode=thread" part from the redirect.

Thanks for your help!

Update: Here's some other code that is already in the .htaccess file.

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
2

There are 2 answers

4
xcvd On

In your .htaccess file:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^mode=thread
RewriteCond %{QUERY_STRING} &id=([0-9]+)
RewriteRule ^index\.php$ /threads/%1? [L,R=301]
0
Wendell On

After a lot of Googling, I found this to work:

RewriteCond %{QUERY_STRING} (^|\?)mode=thread&id=([0-9]+)($|&)
RewriteRule ^index\.php$ http://mywebsite.com/threads/%2/? [R=301,L]