Here's what I'm trying to achieve:
The browser request: example.com/webApp/controller/action/?param1=param1value
- Where the static (always the same) part of the URL is
webApp/
- Where the dynamic part of the URL is
controller/action/?param1=param1value
. Usually it consists ofREQUEST_URI
(path consisting of varying amount of parts) andQUERY_STRING
, and it can be anything, for example:site/index/?page=2
books/index/?page=2&sort=name
users/list
foo/bar
user/profile/posts/?page=2
- The URLs listed above should look like this after .htaccess (what the server receives):
index.php?path=site/index/page/2
index.php?path=books/index/page/2/sort/name
index.php?path=users/list
index.php?path=foo/bar
index.php?path=user/profile/posts/page/2
- While the user sees the URLs without the
index.php
part in their browser:site/index/page/2
books/index/page/2/sort/name
users/list
foo/bar
user/profile/posts/page/2
Basically I want to clean the QUERY_STRING
part of the URL by replacing all ?
and &
characters with /
and append it to the REQUEST_URI
dynamically.
I've been doing a lot of searches regarding this topic. Unfortunately the closest matches were examples of hardcoded QUERY_STRING
parameters turned into paths. Instead I want to turn all QUERY_STRING
parts (if there's any) into a path, dynamically.
My current .htaccess file content is the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js)$
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]
The only missing part is cleaning the QUERY_STRING
, as explained above. Also if the request URI is a file as listed in the code above (list of extensions), it will not be processed via index.php
.