4

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>

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 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!

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
traq
  • 281
  • 2
  • 11

2 Answers2

5

Use an environment variable for the user that runs Apache,

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

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]
Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • I discovered this possibility a while ago, and it would solve my problem - but unfortunately, this is for a project that will be distributed, and I can't be sure that users will all have access to configure Apache. In fact, I'm pretty sure most of them won't (shared hosting). Thanks anyway! I appreciate it! – traq Jul 04 '12 at 02:51
  • That fake Rewrite (I just now noticed your edit from August) is _trick_. I had no idea you could do something like that. Thanks for coming back to this! – traq Sep 06 '13 at 01:51
  • Yes, I was working on something where this came up again and decided to update the post. Very useful indeed. – Paul Sweatte Sep 06 '13 at 01:54
1

I have found a solution to use rewrite base's value. It's only one dot. . This is working. It forwards to rewritebase if rewritebase defined. If rewritebase didn't defined, it forwards to code's directory.

RewriteRule ^index.html(/?)$ ./ [R=301,L]
eyaktas
  • 21
  • 4
  • This may work for you, but it is not using the rewrite base's value. The dot before the slash means "current directory". That's important to point out. Depending on the RewriteRule it can make things worse. – ReynekeVosz May 25 '18 at 09:37