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

51 Views Asked by At

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?

2

There are 2 best solutions below

4
anubhava On BEST ANSWER

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]
0
Daniel Ferradal On

Query Strings (everything after the ? character) are never matched by a RewriteRule, you need a RewriteCond to check the query string

I think you could go with something simple like this, it depends if those variables are always numbers or something else.

RewriteCond %{QUERY_STRING} ^cat_page_(\d+)=(\d+)$
RewriteRule ^ /cat-page/%1/%2.html [R,L]

PS: sorry I didn't get in time at #httpd to help.