.htaccess rewriteRule和rewriteCond无法正常工作 - 为什么?

时间:2014-10-26 11:19:34

标签: regex apache .htaccess mod-rewrite redirect

如果请求的文件存在,我尝试以下方法重定向到php脚本,如果没有,则重定向到missed.php。 为什么不起作用?即使文件存在,它总是会重定向到missed.php

########################## if file exists #########################
# get requested filename
RewriteCond %{REQUEST_FILENAME} (.+)$
# use filename from line above and see if filename.php exists
RewriteCond %1\.php -f
# page standalone && includes index.php
RewriteRule (.+) $1.php [L]
# page included in index.php
#RewriteRule (.+) index.php?p=$1 [L]


#################### if file doesnt exits ################### 
# get requested filename
RewriteCond %{REQUEST_FILENAME} (.+)$   
# if filename.php doesnt exist
RewriteCond %1\.php !-f     
# redirect to missed.php
RewriteRule (.+) missed.php [L]

1 个答案:

答案 0 :(得分:0)

请尝试使用这些规则,因为REQUEST_FILENAME不适用于不存在的文件:

RewriteEngine On
RewriteBase /test/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/test/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ missed.php [L]