重写规则,如果重写的文件不是index.php,则当规则与[L]匹配时不会停止

时间:2019-03-25 02:44:37

标签: apache .htaccess mod-rewrite

我有一个非常复杂的.htaccess文件,其中有很多RewriteRules,除了我尝试重写为非index.php的文件时,它们都运行良好

我尝试添加[L]我正在使用xampp局部运行它,index.php和deleteItem.php都是有效文件。值得注意的是我的规则我不再直接指向PHP文件。

这是我的.htaccess文件

<IfModule mod_rewrite.c>
  RewriteEngine On

  #RewriteBase /smartsharing/community/
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteRule ^.*?([^/]*)/members$ index.php?action=members&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/members/$ index.php?action=members&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/members/edit$ index.php?action=editMembers&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/members/edit/$ index.php?action=editMembers&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/edit$ index.php?action=editItems&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/edit/$ index.php?action=editItems&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/purchasehistory$ index.php?action=purchaseHistory&id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/purchasehistory/$ index.php?action=purchaseHistory&id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/edit$ index.php?action=editItem&id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/edit/$ index.php?action=editItem&id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/remove$ deleteitem.php?id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/items/([^/]*)/remove/$ deleteitem.php?id=$2 [L,QSA]
  RewriteRule ^.*?([^/]*)/edit$ index.php?action=edit&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/edit/$ index.php?action=edit&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]+)$ index.php?action=details&id=$1 [L,QSA]
  RewriteRule ^.*?([^/]*)/$ index.php?action=details&id=$1 [L,QSA]

</IfModule>

我的网址类似于http://localhost/smartsharing/community/communityname/items/2/remove

我希望重写的URL为http://localhost/smartsharing/community/deleteItem.php?id=2 但相反,它似乎去 http://localhost/smartsharing/community/index.php?action=details&id=2

另外,当发布到http://localhost/smartsharing/community/doAction.php时,它会重定向到http://localhost/smartsharing/community/index.php?action=details&id=doAction.php

1 个答案:

答案 0 :(得分:2)

引起问题的不是L,而是您的规则RewriteRule ^.*?([^/]+)$ index.php?action=details&id=$1 [L,QSA]

规则中的全部捕获模式^.*?([^/]+)$与所有重写的URI匹配,并将它们重写为/index.php?action=details&id

发生的事情是,当您转到:

/foobar/items/2/remove 

以下规则匹配

RewriteRule ^.*?([^/]*)/items/([^/]*)/remove/$ deleteitem.php?id=$2 [L,QSA]

然后,要加载目标网址/deleteitem.php?I'd=124,您的服务器将再次读取htaccess文件,并且符合以下规则

  RewriteRule ^.*?([^/]+)$ index.php?action=details&id=$1 [L,QSA]

服务器继续读取htaccess文件,直到找不到匹配的规则。

要解决此问题,您可以在每个规则中使用END标志而不是LL替换ENDEND标志可确保该规则仅在当前重写周期中运行,或者将RewriteConds置于规则之上,以免重写现有文件或URI。

RewriteCond %{REQUEST_FILENAME} !-f