如果.php文件不存在,则重写到另一个文件

时间:2013-07-13 19:29:16

标签: .htaccess mod-rewrite

我使用以下规则来提供关联的.php文件

所有最后的目录路径都将关联php文件Ex。 / about /或/ about将去/about.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`

所以当我使用http://example.com/abc指向/abc.php时,这样可以正常工作。但现在想要更多的逻辑

如果该目录中不存在/abc.php文件,则转到/listing.php文件。我能这样做吗?请帮忙

我只需要这个根目录意味着http://example.com/..here ..而不是任何子目录。有什么建议吗?

由于

1 个答案:

答案 0 :(得分:0)

试试这个,应该测试一下:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php

更新:我在本地服务器上对此进行了测试,请注意:该文件夹是测试的,您可以根据自己的需要进行调整

Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
相关问题