Question

I check out many questions but didn't get my answer so I posted my problem as question I am having issue with rewriting url with htaccess.

My URL : localhost/index.php?api=param/sub_param&some_key=some_value&some_key2=some_value2

How I want it to rewrite : localhost/api/param/sub_param?some_key=some_value&some_key2=some_value2

I know the url structure is bit not correct but still I want like this

RewriteEngine On
RewriteRule api/([a-z0-9_]+/[a-z0-9_]+)?([a-z0-9_])=([a-z0-9_])&([a-z0-9_])=([a-z0-9_]) index.php?api=$1&$2=$3&$4=$5

I don't know why my code is not working.

It works if I use / instead of question I think the only reason its not working is question mark, but I need it.

Was it helpful?

Solution

You earlier rule wasn't working because you cannot match QUERY_STRING using RewriteRule. In any case you don't even need to match/capture query string as you just need QSA flag here. QSA (Query String Append) flag preserves existing query parameters while adding a new one.

You correct rule will be (with more correct regex and flags):

RewriteRule ^api/([a-z0-9_]+/[a-z0-9]+)/?$ index.php?api=$1 [QSA,NC,L]

OTHER TIPS

I got answer by my own I don't how its working if anyone can explain it to so I can select his answer

Working code :

RewriteRule api/([a-z0-9_]+/[a-z0-9]+) index.php?api=$1 [QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top