根据http://htaccess.madewithlove.be/,以下规则集可以完美运行:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^images/([^\?]+)$ /_lib/mask_images.php [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3&page=$4 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&brand=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&page=$2 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&brand=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)$ /search.php?keyword=$1&merchant=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3 [L]
RewriteRule ^search/([^\?]+)$ /search.php?keyword=$1 [L]
RewriteRule ([^\?]+)/productID/([^\?]+)$ /detail.php?keyword=$1&productID=$2 [L]
RewriteRule ([^\?]+)$ /detail.php?keyword=$1 [L]
问题在于,当应用并匹配最后两个规则时,Apache会重新应用匹配的规则(以及正确分配的参数)并进入无限循环。
我补充说:
RewriteCond %{REQUEST_URI} !search.php$
RewriteCond %{REQUEST_URI} !detail.php$
RewriteCond %{REQUEST_URI} !index.php$
到上面的规则集。再次,经过htaccess测试人员网站的验证和工作,它现在停止处理并输出“detail.php”作为参数。
以下是重写日志所说的内容:
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] rewrite 'hello' -> '/detail.php?keyword=hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) split uri=/detail.php?keyword=hello -> uri=/detail.php, args=keyword=hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] rewrite 'detail.php' -> '/detail.php?keyword=detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) split uri=/detail.php?keyword=detail.php -> uri=/detail.php, args=keyword=detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
正如您所看到的,它匹配正确的规则并且参数设置正确,但由于某种原因,它再次重新应用该内部重定向,并且参数将从应该更改(在此示例中)是“hello”)文件名,detail.php
不知道如何解决这个问题,因为我目前所知道的一切,这应该是有效的,因为/搜索工作非常出色。
修改
这是detail.php包含的内容:
<?
echo $_GET["keyword"]."<br>";
echo $_GET["merchant"]."<br>";
echo $_GET["brand"]."<br>";
echo $_GET["page"]."<br>";
echo $_GET["productID"]."<br>";
exit();
?>
答案 0 :(得分:0)
/detail.php的最终[L]执行(内部)重定向。因此Apache看到了一个全新的请求 - 重新应用规则。
所以选项可能是为了确保规则不适用于/detail.php(即通过确保参数名称有点不同)或者提前预防^ / detail.php并否定。< / p>
http://www.sitepoint.com/mod_rewrite-no-endless-loops/试图用不同的词语来解释这一点。另一种常见方法是使用以下内容:
# Loop stopping snippets.
# Uncomment if there are specific pages to exempt.
# RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
# RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
你在第二遍(在内部重定向之后)检测到这一点并在早期以通用方式保释。
DW传递。
答案 1 :(得分:0)
您的缩进不反映RewriteRule和RewriteCond指令的语义关联。
条件
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
仅在URI模式为^ images /([^ \?] +)$时才适用。你需要的是逻辑上翻转这个或者至少跳过以下10条规则来申请所有 URI模式:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [SKIP=10]
有关详细信息,请参阅我的文章More on using Rewrite rules in .htaccess files。