Search code examples
.htaccessmod-rewriteurl-rewritinghttpd.conffriendly-url

How to re-write this double variables url in htaccess in to pretty url


I just want this URL

example.com/?cat_page_10=2 

into this URL

example.com/cat-page/10/2.html

the above URL has two (double) vairables 10 and 2 are both are flexable/dynamic variables they will change according to requested page.

i tried this

RewriteRule ^cat-page/([^/]*)\.html$ /?cat_page_10=$1 [L]

But result is this

http://example.com/cat-page/2.html

and i want this

example.com/cat-page/10/2.html 

Because 10 is also dynamic variable in url so iw ant both will change according to requested url Anyone help me out where i mistake?


Solution

  • Your existing RewriteRule pattern is not correct as it allows only one / after /cat_page.

    You may use this rule:

    RewriteEngine On
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /\?cat_page_([\w-]+)=([\w-]+)\s [NC]
    RewriteRule ^ /cat-page/%1/%2.html? [R=302,L,NE]
    
    # internal rewrite from pretty URL to actual one
    RewriteRule ^cat-page/([\w-]+)/([\w-]+)\.html$ ?cat_page_$1=$2 [L,QSA,NC]