重写首先重定向到缓存的内容

时间:2012-07-04 04:16:18

标签: caching mod-rewrite

我尝试了很多组合但没有效果。我试着编写重写规则:

  1. 假设http://example.com/project/big_bunny
  2. 的网址
  3. 当用户访问网站重写时首先尝试读取文件/cache/project/big_bunny.html但仅在没有任何获取或发布数据的情况下
  4. 如果没有缓存重写过程标准路由器规则:index.php?_rt = $ 1 [L,QSA]
  5. 有什么建议吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

# Check the cache, and rewrite if file exists
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule ^ /cache%{REQUEST_URI}.html [L]

# If URI doesn't start with /cache/, it wasn't cached so rewrite to index.php
RewriteCond %{REQUEST_URI} !^/cache/
RewriteRule ^(.*)$ index.php?_rt=$1 [L,QSA]

答案 1 :(得分:0)

试试这个:

# Check if using POST method
RewriteCond %{THE_REQUEST} !^POST
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
# Check if cache exists
RewriteCond /cache%{REQUEST_URI}.html -f
# Check if contains any GET query string
RewriteCond %{QUERY_STRING} !.+
# Do the rewrite
RewriteRule ^/(.*) /cache/$1.html [L]

# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule ^/(.*) index.php?_rt=$1 [L,QSA]