是的RewriteBase可用的值作为一个变量/引用?是的、作为一个、变量、RewriteBase

2023-09-02 00:42:30 作者:我深知抛我追狗是你本性

我在写一个.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/]

由于隐含的缓存/ 目录将始终位于的RewriteBase指定的目录中。 。 。 。但是,它的没有的始终,其中该.htaccess文件存在的物理路径。 [/编辑]

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!

推荐答案

使用环境变量的user运行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]

一个伪重写规则还可以做的伎俩:

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]