htaccess仅在请求的URL中存在文件时才加载template.php文件

时间:2016-01-21 17:32:12

标签: php json .htaccess

我想让htaccess加载一个充当我的前端控制器的template.php文件,如果所请求的URL中存在一个gallery.json文件,则

例如,如果我去了mysite.com/test并且在test文件夹中我有一个gallery.json文件,我想要template.php来读取该gallery.json文件

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/test/gallery.json  -f
RewriteCond %{REQUEST_URI} !/test/template.php
RewriteRule  ^(.*)  /test/anormalfile.php [L]

在这里,您可以检查测试目录中是否存在gallery.json文件。如果是,则加载template.php,否则加载anormalfile.php。希望这有助于你。

对您最有用的RewriteRule标志称为Skip。它类似于大多数编码语言中的if语句。它将进行检查,然后忽略一定数量的行,具体取决于你告诉它做什么。您可以阅读更多相关信息here