使用mod_rewrite从常规URL查看缓存的版本 [英] Using mod_rewrite to view cached version from usual URL

查看:78
本文介绍了使用mod_rewrite从常规URL查看缓存的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP站点生成动态db驱动的代码的静态html版本,并将它们存储在名为cache的文件夹中.

这意味着当您访问说/about-us/时,请求将通过index.php?page = about-us路由,并生成一个名为/cache/about-us.html的文件.

如果该文件存在,PHP将包含它,然后退出.这似乎是在浪费时间,为什么不只在请求/about-us/时让apache提供/cache/about-us.html的服务,而是在存在的情况下提供服务.

到目前为止,我当前的mod_rewrite部分包括以下内容:

RewriteRule ^([A-Za-z0-9-_/\.]+)\/$ /?page=$1 [L]

哪个/foo_bar/请求写入index.php?page = foo_bar.在此之前,我可以放什么来请求我的缓存版本?

解决方案

首先将所有内容发送到/cache/文件夹

RewriteRule ^([A-Za-z0-9-_/\.]+)/$ /cache/$1

然后,检查是否找不到,然后重新路由

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/cache/([A-Za-z0-9-_/\.]+)$ /page?=$1 [L]

根据服务器的设置方式,您可能必须稍微更改路径(我建议使用绝对路径).另外请注意,这也将适用于与RewriteRule匹配的图像

我不确定您是否还会因此而获得显着的性能提升-仅包含静态文件就不会花费那么长时间(如果您不访问数据库).另外,如果您具有 APC ,则可以将文件缓存在内存中.

My PHP site generates static html versions of dynamic db driven code and stores them in a folder called cache.

This means that when you visit say, /about-us/, the request is routed through index.php?page=about-us, and produces a file called /cache/about-us.html.

If that file exists, the PHP includes it, then exits. This seems a waste of time, why not just get apache to serve up /cache/about-us.html when /about-us/ is requested, but only if it exists.

My current mod_rewrite section just includes this so far:

RewriteRule ^([A-Za-z0-9-_/\.]+)\/$ /?page=$1 [L]

Which writes any /foo_bar/ request to index.php?page=foo_bar. What can I put before this to request my cached version if it exists?

解决方案

First send everything to the /cache/ folder

RewriteRule ^([A-Za-z0-9-_/\.]+)/$ /cache/$1

Then, check if it's not found, and reroute

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/cache/([A-Za-z0-9-_/\.]+)$ /page?=$1 [L]

Depending on how your server is set up you might have to change the paths a bit (I'd recommend using absolute ones). Also note that this will also apply to images that match the RewriteRule

I'm not sure wether you'll see any significant performance increase with this - just including a static file doesn't take that long (if you don't hit a database). Also, if you have APC you could cache the files in-memory.

这篇关于使用mod_rewrite从常规URL查看缓存的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆