0

我正在尝试使用 mod_rewrite 将文件发送到 engine.php 文件。检查是否存在带有 .php 扩展名的请求文件时遇到错误。我会在代码之后解释更多:

Options -Indexes +FollowSymlinks -MultiViews

<IfModule mod_rewrite.c>
RewriteEngine On

# If the requested filename is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# ... but the requested filename.php is a file
RewriteCond %{REQUEST_FILENAME}.php -f
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1.php [QSA,L]

# If the requested script filename is a directory
RewriteCond %{SCRIPT_FILENAME} -d
# ... and an index file exists
RewriteCond %{SCRIPT_FILENAME}index.php -f
# .. rewrite to the engine
RewriteRule ^(.*)$ engine.php?page=$1index.php [QSA,L]

# If the requested script filename is a directory
RewriteCond %{SCRIPT_FILENAME} -d
# ... and an index file exists
RewriteCond %{SCRIPT_FILENAME}/index.php -f
# .. rewrite to the engine
RewriteRule ^(.*)$ engine.php?page=$1/index.php [QSA,L]

# If the file is a PHP or HTML file
RewriteCond %{SCRIPT_FILENAME} \.(php|html)$
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1 [QSA,L]

# If the script filename is not a file
RewriteCond %{SCRIPT_FILENAME} !-f 
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1 [QSA,L]
</IfModule>

我会声明我仍然有办法使用 mod_rewrite,所以我希望上面的代码不是太糟糕,我的问题不是一些简单的问题。
如果用户请求“目录/文件”,但该文件不存在(例如,页面可以存储在数据库中),它将取决于几个变量:

  • 如果文件“directory.php”存在,它会将“directory/file.php”发送到engine.php
  • 如果文件“directory.php”存在,但目录“directory”也存在,它将向引擎发送“directory/file”(根据需要)
  • 如果文件“directory.php”和目录“directory”都不存在,它将向引擎发送“directory/file”(根据需要)

如您所见,错误是文件“directory.php”存在时。
希望这是我犯的一个简单错误,我可以从中吸取教训。我想我们会看到的!
编辑
几个例子,希望能更清楚一点。假设这些都以“http://domain.com/”开头。这些是想要的结果:
输入:“directory”
输出:“directory/index.php”
注意:“engine.php?page=directory/index.php”存在

输入:“directory”
输出:“directory”
注意:“engine.php?page=directory/index.php”不存在

输入:“directory/”
输出:“directory/indes.php”
注意:“engine.php?page=directory/index.php”存在

输入:“directory/”
输出:“directory/”
注意:“engine.php?page=directory/index.php”不存在

输入:“file”
输出:“file.php”
注意:“engine.php?page=file.php”存在

输入:“file”
输出:“file”
注意:“engine.php?page=file.php”不存在

4

0 回答 0