Home > Enterprise >  htaccess rewrite key/value pair params in url
htaccess rewrite key/value pair params in url

Time:01-18

I know this is very common question but I can't seem find any solution regarding in my problem. The folder structure looks like this:

- rootProject
  - subfolder
    * .htaccess
    * index.php

Normal url params: http://website.com/subfolder/?dynamicKey=dynamicValue. But What I want to achieve is like this: http://website.com/subfolder/dynamicKey/dynamicValue

But I failed to do that, I can only retrieve the dynamicKey.

Here is my current htaccess:

RewriteEngine On
RewriteRule ^([a-z] )/?$ index.php?$1 [NC,QSA,L]

CodePudding user response:

Thanks to @Ar Rakin. Here is the solution.

RewriteEngine On
RewriteRule ^([a-z] )/?$ index.php?$1 [NC,QSA,L]
RewriteRule ^([a-z] )/([a-z] )/?$ index.php?$1=$2 [NC,QSA,L]

CodePudding user response:

With your shown samples and attempted code, please try following htaccess rules. We need to add conditional checks before rewrite rules else later it will affect your existing pages also.

Make sure to clear your browser cache before testing your URLs.

RewriteEngine On
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z] )/?$ index.php?$1 [NC,QSA,L]

##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z] )/([a-z] )/?$ index.php?$1=$2 [NC,QSA,L]

NOTE: your used regex [a-z] will only match small letters in uri, in case it needed to check any alphabets(capital OR small) then change FROM [a-z] TO [a-zA-Z] in above rules also.

  •  Tags:  
  • Related