php - HTACCESS: Dynamic rewrite url with multiple variables -


i've been working on couple of days , i'm wondering if point me in right direction please... i'm trying pass 2 variables in rewrite.

unless given url file or directory, i'd urls dynamically rewritten follows:

example.com/category -> example.com/index.php?p=category
example.com/category/page -> example.com/index.php?p=category&article=page
example.com/category2/page -> example.com/index.php?p=category2&article=page

i'm having hard time trying turn following in dynamic:
rewriterule ^category/(.+?)$ ?p=category&article=$1 [l]

i'm trying doesnt work:
rewriterule ^(.+?)/(.+?)$ ?p=$1&article=$2 [l]

...also trying doesnt work either:
rewriterule ^(.*)/(.+?)$ ?p=$1&article=$2 [l]

i'm trying avoid putting in following make work:
rewriterule ^category2/(.+?)$ ?p=category2&article=$1 [l]

here full code:

options +followsymlinks rewriteengine on  rewriterule ^category/(.+?)$ ?p=category&article=$1 [l]  rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)$ ?p=$1 [l] 

update:

this did me:

rewriterule ^([^/]+)(/([^/]+))? ?p=$1&article=$2 [l] 

thank you!! andrew

i'm not expert think you:

rewriteengine on  rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f rewriterule ([^/]+)/([^/]+) index.php?p=$1&article=$2         [l] 

where [^/]+ matches except /, result when tested on htaccess.madewithlove.be

enter image description here


edit:

as totally forgot second parameter optional, great update in op did except fact adds / @ start of parameter2 value, because $2 capturing outer group, while need inner one, shown in image:

enter image description here


instead replace $2 $3 , should fix it:

rewriterule ^([^/]+)(/([^/]+))? ?p=$1&article=$3   [l] 

enter image description here


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -