如果文件不存在则重写

时间:2015-11-02 21:15:20

标签: .htaccess

我的.htaccess文件中有以下代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ /pages.php?link=$1

我的理解是它应该检查文件是否不存在,检查目录是否不存在然后执行规则(如果这些是真的)。 出于某种原因,虽然我去的时候给了我一个错误:

http://localhost/1.php

弹出的错误消息是:

script 'C:/xampp/htdocs/1.php' not found or unable to stat

奇怪的是,当我去的时候: http://localhost/1

它按预期行事(相应地重定向)。

1 个答案:

答案 0 :(得分:1)

我相信你的问题就是你的正则表达式。您正在使用\w来匹配单词。好.与此不匹配。将您的规则更改为此。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pages.php?link=$1