我有一个包含此代码的'.htaccess'文件:
(它应该重定向像"P7-product_number_7"这样的名称)
RewriteEngine On
RewriteBase /
# if:
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
# then:
RewriteRule ([A-z])([0-9]+)-([^/]*)?$ /cache%{REQUEST_URI}.html [B,L]
# else:
RewriteRule ([A-z])([0-9]+)-([^/]*)?$ index.php?type=$1&id=$2&label=$3 [B,L]
检查文件的现有(在'cache'目录中)工作正常,但重定向不起作用 .
Why ?
我试图使用“ RewriteRule ^(.*)$ /cache/$1.html [L] ”而不是第一个RewriteRule,但它也没有用 .
(如果HTML文件没有重定向't exist (in /cache/) works fine , the ' .htaccess ' and ' cache / ' directory are in the ' / var / www'目录,那么%是"/P7-product_number_7")
1 回答
您的规则是循环的,因为您没有检查URI的开头 . 您可能会注意到
label
$_GET 参数是 product_number_7.html ,最后是.html
. 这意味着正在应用第一个规则,然后规则循环,但第二个条件第二次失败(因为没有P7-product_number_7.html.html
),因此第二个规则被应用:URI =
/P7-product_number_7
条件检查:
%{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
- > true应用规则:匹配
([A-z])([0-9]+)-([^/]*)?$
- > trueURI重写为
/cache/P7-product_number_7.html
start URI
/P7-product_number_7
!=/cache/P7-product_number_7.html
,重写引擎循环:URI =
/cache/P7-product_number_7.html
条件检查:
%{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
- > false (/cache/cache/P7-product_number_7.html.html
不存在)应用规则:匹配
([A-z])([0-9]+)-([^/]*)?$
- > true(匹配URI的/ cache / P7-product_number_7 .html部分)URI被重写为
/index.php?type=P&id=7&label=product_number_7.html
因此,您需要在规则的开头添加一个不再重写的passthrough:
或者您可以简单地匹配正则表达式中URI的开头: