Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

Rewriting URLs with htaccess, multiple parameters


I'm trying to rewrite something like this:

https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here

into

https://mywebsite.com/pages/article/1/Title-Goes-Here

Using this Rewrite Rule

RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.html?id=$1&title=$2 [NC,L] 

However, when I try this code in https://htaccess.madewithlove.com/ it gives me

This rule was not met.

Also tried it on my website htaccess file with no result. I don't know where is the problem.


Solution

  • Please don't create OR test rules on online sites, they are NOT trust worthy, so kindly test these rules into your localhost OR apache.

    With your shown samples/attempts, please try following htaccess rules. Considering that you are hitting URL https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here in browser AND you want to redirect it to URL https://mywebsite.com/pages/article/1/Title-Goes-Here in browser.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##External redirect in browser rules here....
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{THE_REQUEST} \s/([^/]*)/([^.]*)\.html\?id=([^&]*)&title=(\S+)\s [NC]
    RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
    
    ##Internal rewrite to html file rules here....
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$  $1/$2.html?id=$3&title=$4 [QSA,NC,L]