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

浪尽此生 提交于 2020-01-02 08:52:11

问题


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?


回答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]



回答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.



来源:https://stackoverflow.com/questions/20260571/what-does-rewriterule-path-1-qsa-l-mean-in-my-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!