Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

Htaccess Redirect with Parameters split at


Is this by Redirect or RewriteRule possible with a htaccess?

This is old url:

mysite.com/showthread.php?28151-my-car&p=10

And this shoud be new one:

mysite.com/index.php?threads/28151/page-10

Thanks!

I have tried but couldn't split first parameter "28151-my-car" to only ID number(28151). I dont know which one is better, rewriterule or redirect.

RewriteRule showthread.php?$1&$2$ index.php?threads/(.*) [L]


Solution

  • With your shown samples and attempts please try following .htaccess rules file. This treats you need to internal rewrite the URL and make sure .htaccess rules file and index.php file should be in same place. Make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{THE_REQUEST} \s/showthread\.php\?(\d+)-my-car&p=(\d+)\s [NC]
    RewriteRule ^ index.php?threads/%1/page-%2 [L]
    

    Generic Rules: Where its not matching literal my-car in spite of that it matches anything before &

    RewriteEngine ON
    RewriteCond %{THE_REQUEST} \s/showthread\.php\?(\d+)-[^&]*&p=(\d+)\s [NC]
    RewriteRule ^ index.php?threads/%1/page-%2 [L]