在一个文件中包含多个ht访问规则


multiple ht access rules in one file

我的站点应该尝试从缓存子目录(/app/storage/cache)提供所有请求,以便从/app/storage/cache/two.html提供/two.html请求

更多的例子:/service from/app/storage/cache/index.html/app/storage/cache/two.html/app/storage/cache/folder/index.html

如果没有找到文件/目录,它应该用php文件/app/router.php

处理所有请求

我试过了:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]
RewriteRule ^(/)?$ /app/storage/www/$1 [L]

可以很好地捕获所有url并在不更改url的情况下提供正确的资源。现在要捕获所有其他内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . app/router.php [L]

同样有效,但是第一组条件不再有效…

我认为有三种方法可以做到这一点。一种是在通过同一个文件生成页面之前,通过router.php检查缓存。另外两种方法涉及一些重写魔法。

我没有测试过这两个

失败方法

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1
#From this point on, the file/dir either exists or
#%{REQUEST_FILENAME} is now /app/storage/www/something
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/storage/www/ /app/router.php [L]

明确测试

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-d
RewriteRule ^(.*)$ /app/router.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]

这两种方法都可以通过使用Apache的主配置文件(httpd.conf)而不是。htaccess文件来加快速度,因为它不需要在每个请求时搜索/读取。htaccess文件。