在rewriten文件路径重写规则检查文件是否存在 [英] RewriteRule checking file in rewriten file path exists
问题描述
如何使用ModRewrite检查一个缓存文件存在,如果是这样,重写缓存文件否则改写为一个动态的文件。
例如我有以下文件夹结构:
pages.php
缓存/
页/
1.HTML
2.HTML
textToo.html
等等
你会如何设置此所以请求的RewriteRules可以发送这样的:
example.com/pages/1
和如果高速缓存文件是否存在重写托特缓存文件,并且如果高速缓存文件不存在,改写到pages.php?p为1
应该是这样的:(注意,这是不行的,不然我也不会问这个)
重写规则^页/([^/\\.]+)缓存/页/ $ 1.HTML [NC,QSA]
的RewriteCond%{} REQUEST_FILENAME -f [NC,OR]
的RewriteCond%{} REQUEST_FILENAME -D [NC]
重写规则缓存/页/([^/\\.]+)HTML pages.php?p = $ 1 [NC,QSA,L]
我能够把粗做到这一点使用PHP,但我认为它必须使用mod_rewrite就可以了。
重写规则^页/([^/\\.]+)缓存/页/ $ 1.HTML [NC,QSA ]#在这一点上,我们已经重新编写页/ 4缓存/页/ 4.html
的RewriteCond%{} REQUEST_FILENAME!-f#如果上述的RewriteCond成功,我们没有一个高速缓存,所以重写
#在pages.php URI,否则我们脱落月底与去
#缓存/页/ 4.html
重写规则^缓存/页/([^/\\.]+)HTML pages.php?p = $ 1 [NC,QSA,L]
关闭多视图是至关重要的(如果你有他们启用)为好。
选项-MultiViews
否则初始请求(/页/ ...)将获得自动转换的mod_rewrite在踢之前/pages.php,你也可以只重命名pages.php到别的东西(和更新的最后一个重写规则以及)以避免多视图冲突
编辑:我最初包括的RewriteCond ... -d
但它是多余的。
How can you use ModRewrite to check if a cache file exists, and if it does, rewrite to the cache file and otherwise rewrite to a dynamic file.
For example I have the following folder structure:
pages.php cache/ pages/ 1.html 2.html textToo.html etc.
How would you setup the RewriteRules for this so request can be send like this:
example.com/pages/1
And if the cache file exists rewrite tot the cache file, and if the cache file does not exists, rewrite to pages.php?p=1
It should be something like this: (note that this does not work, otherwise I would not have asked this)
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]
I can off coarse do this using PHP but I thought it had to be possible using mod_rewrite.
RewriteRule ^pages/([^/\.]+) cache/pages/$1.html [NC,QSA]
# At this point, we would have already re-written pages/4 to cache/pages/4.html
RewriteCond %{REQUEST_FILENAME} !-f
# If the above RewriteCond succeeded, we don't have a cache, so rewrite to
# the pages.php URI, otherwise we fall off the end and go with the
# cache/pages/4.html
RewriteRule ^cache/pages/([^/\.]+).html pages.php?p=$1 [NC,QSA,L]
Turning off MultiViews is crucial (if you have them enabled) as well.
Options -MultiViews
Otherwise the initial request (/pages/...) will get automatically converted to /pages.php before mod_rewrite kicks in. You can also just rename pages.php to something else (and update the last rewrite rule as well) to avoid the MultiViews conflict.
Edit: I initially included RewriteCond ... !-d
but it is extraneous.
这篇关于在rewriten文件路径重写规则检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!