【问题标题】:Rewrite Rules, not stopping when rule is matched with [L] if rewritten file is not index.php重写规则,如果重写的文件不是 index.php,则当规则与 [L] 匹配时不停止
【发布时间】:2019-03-25 02:44:37
【问题描述】:

我有一个非常复杂的 .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

【问题讨论】:

  • 我个人讨厌将.htaccess 用于复杂的事情。我不太擅长。我将每个请求重定向到/handle_request.php(内部),并且我有一个路由器可以根据 URL 确定要做什么。并不是说您必须切换到那种做事方式。但这就是我所做的。 .htaccess 快把我逼疯了,哈哈

标签: apache .htaccess mod-rewrite


【解决方案1】:

不是L 而是你的规则RewriteRule ^.*?([^/]+)$ index.php?action=details&amp;id=$1 [L,QSA] 导致了问题。

规则中的包罗万象的模式 ^.*?([^/]+)$ 匹配所有重写的 URI 并将它们重写为 /index.php?action=details&amp;id

发生的事情是当你去:

/foobar/items/2/remove 

以下规则匹配

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

然后,要加载目标 url /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

【讨论】:

  • 感谢您解释发生了什么,您的解决方案有效。
猜你喜欢
  • 2011-07-18
  • 2020-01-18
  • 2015-09-24
  • 1970-01-01
  • 2018-04-26
  • 2013-12-16
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
相关资源
最近更新 更多