是RewriteBase可用作变量/引用的值吗? [英] is the value of RewriteBase available as a variable/reference?

查看:344
本文介绍了是RewriteBase可用作变量/引用的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个.htaccess文件,该文件将检查请求的页面是否存在于缓存中。为了执行检查(并保存打字),我设置一个带缓存位置的ENV变量:

I'm writing an .htaccess file that will check if a requested page exists in a cache or not. In order to perform the check (and save typing), I'm setting an ENV variable with the location of the cache:

# all this works as I expect #
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /some/path/
RewriteRule ^(.*)$ - [E=rewritebase:/some/path/,E=cache:%{ENV:rewritebase}cache/] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{ENV:cache}$1\.html -f
RewriteRule ^(.*)$ $1\.html [L]

</IfModule>

如您所见,我还必须设置一个ENV变量来 RewriteBase值。我宁愿不是,因为如果RewriteBase改变了,我也要记住改变ENV变量。最终,这可能是其他人使用的CMS的一部分,我希望尽可能简单/直接配置,错误的机会很少。我想只能设置 ENV:缓存变量,而不需要设置 ENV:rewritebase 变量,像这样(或类似):

As you can see, I'm also having to set an ENV variable to "stand in" for the RewriteBase value. I'd rather not, since if the RewriteBase is changed, I'd have to remember change the ENV variable also. Eventually, this may be a part of a CMS used by others, which I wish to be as simple/straightforward as possible to configure, with the fewest opportunities for error. I'd like to be able to set only the ENV:cache variable without the need for setting an ENV:rewritebase variable, like so (or similar):

# doesn't work #
RewriteRule ^(.*)$ - [E=cache:%{RewriteBase}cache/]

As暗示, cache / 目录将始终位于RewriteBase中指定的目录内。


。 。 。然而,它不会
始终是这个.htaccess文件存在的物理路径。

[/ edit]

As implied, the cache/ directory will always be located inside the directory specified in RewriteBase. [edit]
. . . however, it will not always be the physical path where this .htaccess file exists.
[/edit]

我也很乐意听到其他建议。谢谢大家!

I'd be happy to hear alternative suggestions, as well. Thanks, everyone!

推荐答案

使用环境变量为运行Apache的用户

#.profile of Apache user
rewritebase = "some/path"

然后在.htaccess文件中引用它:

then refer to it inside your .htaccess file:

#.htaccess file
RewriteBase {$rewritebase}    
RewriteRule ^(.*)$ - [E=cache:%{ENV:rewritebase}cache]

一个伪ReWriteRule也可以做到这一点:

A pseudo ReWriteRule can also do the trick:

#This will be true for any user agent
RewriteCond  %{HTTP_USER_AGENT}  ^.*

#Replace / with / and set the rewritebase variable to /some/path
RewriteRule  /(\/some\/path)* / [E=rewritebase:$1/some/path]

#Reference the rewritebase variable
RewriteBase {$rewritebase}

#Redefine the rewritebase variable
RewriteRule ^(.*)$ - [E=rewritebase:$1]

这篇关于是RewriteBase可用作变量/引用的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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