I have the following .htaccess Rewrite rule below which works for converting virtual directories to parameters, for example:
www.example.com/usa/ny/nyc gets interpreted by PHP as www.example.com/index.php?path=usa/ny/nyc.
What I can’t seem to figure out is how I would change my regex below to handle parameters of the virtual directories themselves. For example, I want:
www.example.com/usa/ny/nyc/?display=off&settings=none to be seen by PHP as www.example.com/index.php?path=usa/ny/nyc¶m=display:off,settings:none.
What makes it extra tricky is that the parameters won’t always be those two options I used in the example above, they will change dynamically. Any ideas or suggestions of how to go about accomplishing this?
RewriteRule ^/?([a-zA-Z_\-/]+)$ index.php?path=$1 [L]
Assuming you want to pass the query string unmodified, you can use the
[QSA](query string append) option like so:You can find the documentation for the
QSAoption here. From the docs:So, your PHP script will see all the parameters as standard
_$_GETparameters, rather than needing to do any other modification.If you would prefer to treat the result more like a typical path element, you can use the following:
In the above case, your query string will still be appended, however you will need to handle the path explicitly using
$_SERVER['PATH_INFO'].