1

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 of REQUEST_URI (path consisting of varying amount of parts) and QUERY_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.

Janne252
  • 11
  • 2

1 Answers1

0

I have partially solved the issue by using the following .htaccess code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js)$

RewriteCond %{QUERY_STRING} ^(.*)=(.*) [OR]
RewriteRule ^([^?]*)$ index.php?path=$1&%1=%2 [NC,L]
  • QUERY_STRING is no longer appended at the end of the URL (QSA removed from the RewriteRule).
  • Instead, QUERY_STRING is matched with the condition RewriteCond %{QUERY_STRING} ^(.*)=(.*) [OR] and later used by including tokens %1 and %2 in the RewriteRule.
  • This solution DOES NOT remove the GET parameters from the URL, but it does make sure they don't end up in path parameter.
    • Instead, the query string works normally and creates its own GET parameter entries (because it now starts with & instead of ?. ? is already used for the parameter path.
    • The situation described in the first post leads to a case where the query string is malformed because the "first parameter character ?" is used twice. At least this my assumption..
Janne252
  • 11
  • 2