Search code examples
.htaccessurlquery-string

about ngnix / htaccess config


i need to get querystring end of the path all path define a url variable

www.root.com/path1/path2/path3/page?id=15&do=post

i can get all path to variable but i can not get the querystring "id" and "do"

my config is here

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Solution

  • I would expect your implementation with the QSA flag to work as expected. A simple htaccess tester confirms that (see "The new url ..." at the bottom). So you might want to investigate why your tests came out different...

    However there are alternatives, you could also actively capture the query string. The documentation of the apache rewrite module clearly says that you need a RewriteCond to capture the query string of a requested URL.

    This might point you into the right direction:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^id=(\d+)&do=(\w+)$
    RewriteRule ^(.+)$ index.php?url=$1&id=%1&do=%2 [L]
    

    That would be a simplified version:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^id=(\d+)&do=(\w+)$
    RewriteRule ^ index.php?url=%{REQUEST_URI}&id=%1&do=%2 [L]
    

    Or even simpler, if that is enough for you, since you don't have to capture the query string at all:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php?url=%{REQUEST_URI}&%{QUERY_STRING} [L]