如何使用ModRewrite检查缓存文件是否存在,如果存在,则重写缓存文件,否则重写为动态文件.
例如,我有以下文件夹结构:
pages.php cache/ pages/ 1.html 2.html textToo.html etc.
你如何为此设置RewriteRules,所以请求可以像这样发送:
example.com/pages/1
如果缓存文件存在,则重写缓存文件,如果缓存文件不存在,则重写为pages.php?p = 1
它应该是这样的:(请注意,这不起作用,否则我不会问这个)
RewriteRule ^pages/([^/\.]+) cache/pages/$1.html [NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule cache/pages/([^/\.]+).html pages.php?p=$1 [NC,QSA,L]
我可以使用PHP来粗略地做这个,但我认为必须使用mod_rewrite.
我已经使用下面的.htaccess代码为移动/桌面重定向提供了一个不错的工作解决方案,但是如果有人可以提供帮助,还有一些可以真正实现最终的增强功能.
目录设置:
Cookie启用链接:
当前.htaccess文件:
RewriteEngine on
# Check if this is the desktop to mobile query string
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
# Set a cookie, and skip the next 2 rules
RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=2]
# Check if this is the mobile to desktop query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Check if we're not already …Run Code Online (Sandbox Code Playgroud)