.htaccess RewriteCond%{REQUEST_FILENAME}!-d无效

时间:2014-10-10 07:25:37

标签: php .htaccess url-rewriting

.htaccess代码是,

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L] #
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #

我正在尝试访问服务器中已存在的http://example.com/directory/但我无法访问它。没有任何错误发生,但是,如果有已经存在的目录的请求,则不应该应用RewriteCond %{REQUEST_FILENAME} !-d之后的规则。

但在这种情况下,3条规则中的一条正在适用。

RewriteCond %{REQUEST_FILENAME} !-d似乎无效。

1 个答案:

答案 0 :(得分:2)

RewriteCond仅适用于下一个RewriteRule,而不适用于所有RewriteRule。然后你应该这样复制它们:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #

另一种解决方案是使用S标志(跳过)并反转RewriteCond:Apache doc for S flagexamples(法文文章,但htaccess片段不需要翻译:))


使用其他解决方案编辑(未经测试):

# if it's a file or a folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [S=3] # then skip the next 3 rules
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L]
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L]