What does RewriteRule ^(.*)/$ ?path=$1 [QSA,L] mean in my .htaccess?

15,332

Solution 1

RewriteCond % !-f seems incorrect rule condition and is always evaluating to true.

This rule:

RewriteRule ^(.*)/$ ?path=$1  [QSA,L]

Is matching any URI with trailing slash and internally rewriting to /?path=uri-without-slash

So for ex: an URI /foo/ will be rewritten to /?path=foo

  • QSA - Query String Append
  • L = Last rule

Reference: Apache mod_rewrite Introduction

UPDATE: Change that incorrect condition to:

# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]

Solution 2

It means if the request isn't to a file, then rewrite everything before a trailing / into index.php?path= followed by what was previously matched.

It should be the last rule (L) and it should append the query string (QSA), as opposed to discarding it because of the replacement's query string.

Share:
15,332
0pTEC
Author by

0pTEC

Updated on June 04, 2022

Comments

  • 0pTEC
    0pTEC almost 2 years

    I need to create rewrite in nginx as is done in my .htaccess and there are some lines which I don't completely understand.

    DirectoryIndex index.php
    
    RewriteEngine on
    RewriteCond % !-f
    RewriteRule ^(.*)/$ ?path=$1  [QSA,L]
    

    Can someone explain it to me?