RewriteCond %{REQUEST_URI} in subfolder

My URLs are being rewritten as follows

#category and page links
RewriteRule ^([a-z0-9\\-]*-wallpapers)-([0-9]*)\\.html$ index.php?cat=$1&page=$2 [L]
RewriteRule ^([a-z0-9\\-]*-backgrounds)-([0-9]*)\\.html$ index.php?cat=$1&page=$2 [L]

Now in index.php the page generation happens in a way where I could cache the page in a /cache/ folder under the name of REQUEST_URI.

So, when someone requests

/some-wallpapers-1.html

I could cache a copy of the page as

/cache/some-wallpapers-1.html

What I would like to do, is actually perform this cacheing and attempt to rewrite URLs to that cache before sending them to index.php

My first attempt looked something like the following. (im not sure this was exactly it, im para-coding here)

RewriteCond cache/%{REQUEST_URI} -f
RewriteRule ^(/.*)$ cache/$1

But with no success.

I could simply have PHP check the cache and readfile() the content, but I’d like to have Apache just return the existing file if possible.

jb,

That looks like a very smart thing to do - and a good way to do it! :tup:

HOWEVER, I’d first get your regex right! Don’t you NEED to ahve something in your atoms besides -wallpapers?[indent]Hint: The * means ZERO or more whereas + means one or more.[/indent]

RewriteCond /cache/%{REQUEST_URI} -f
RewriteRule .? /cache/%{REQUEST_URI} [L]

… looks like it should do the trick - IF your support files were coded with absolute links (else they would be off one directory level).

Regards,

DK

I set a RewriteLog and RewriteLogLevel to 9 so I could see what’s going on. (note this is my development server)

With the following

RewriteBase /temp

RewriteCond cache%{REQUEST_URI} -f
RewriteRule .? cache%{REQUEST_URI} [L]

RewriteRule test.html index.php?uri=%{REQUEST_URI}

I got this

 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/test.html -> test.html
 (3) [perdir /hdd2/www/temp/] applying pattern '.?' to uri 'test.html'
 (4) [perdir /hdd2/www/temp/] RewriteCond: input='cache/temp/test.html' pattern='-f' => not-matched
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/test.html -> test.html
 (3) [perdir /hdd2/www/temp/] applying pattern 'test.html' to uri 'test.html'
 (2) [perdir /hdd2/www/temp/] rewrite 'test.html' -> 'index.php?uri=/temp/test.html'
 (3) split uri=index.php?uri=/temp/test.html -> uri=index.php, args=uri=/temp/test.html
 (3) [perdir /hdd2/www/temp/] add per-dir prefix: index.php -> /hdd2/www/temp/index.php
 (2) [perdir /hdd2/www/temp/] trying to replace prefix /hdd2/www/temp/ with /temp
 (5) strip matching prefix: /hdd2/www/temp/index.php -> index.php
 (4) add subst prefix: index.php -> /temp/index.php
 (1) [perdir /hdd2/www/temp/] internal redirect with /temp/index.php [INTERNAL REDIRECT]
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/index.php -> index.php
 (3) [perdir /hdd2/www/temp/] applying pattern '.?' to uri 'index.php'
 (4) [perdir /hdd2/www/temp/] RewriteCond: input='cache/temp/index.php' pattern='-f' => not-matched
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/index.php -> index.php
 (3) [perdir /hdd2/www/temp/] applying pattern 'test.html' to uri 'index.php'
 (1) [perdir /hdd2/www/temp/] pass through /hdd2/www/temp/index.php

Now this line got me thinking, and I started playing with the paths

 (4) [perdir /hdd2/www/temp/] RewriteCond: input='cache/temp/test.html' pattern='-f' => [b]not-matched[/b]

Until I got to this

RewriteBase /temp

RewriteCond /hdd2/www/temp/cache%{REQUEST_URI} -f
RewriteRule .? /temp/cache%{REQUEST_URI} [L]

RewriteRule test.html index.php?uri=%{REQUEST_URI}

Which in turn gave me this

 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/test.html -> test.html
 (3) [perdir /hdd2/www/temp/] applying pattern '.?' to uri 'test.html'
 (4) [perdir /hdd2/www/temp/] RewriteCond: input='/hdd2/www/temp/cache/temp/test.html' pattern='-f' => matched
 (2) [perdir /hdd2/www/temp/] rewrite 'test.html' -> '/temp/cache/temp/test.html'
 (2) [perdir /hdd2/www/temp/] trying to replace prefix /hdd2/www/temp/ with /temp
 (1) [perdir /hdd2/www/temp/] internal redirect with /temp/cache/temp/test.html [INTERNAL REDIRECT]
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/cache/temp/test.html -> cache/temp/test.html
 (3) [perdir /hdd2/www/temp/] applying pattern '.?' to uri 'cache/temp/test.html'
 (4) [perdir /hdd2/www/temp/] RewriteCond: input='/hdd2/www/temp/cache/temp/cache/temp/test.html' pattern='-f' => not-matched
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/cache/temp/test.html -> cache/temp/test.html
 (3) [perdir /hdd2/www/temp/] applying pattern 'test.html' to uri 'cache/temp/test.html'
 (2) [perdir /hdd2/www/temp/] rewrite 'cache/temp/test.html' -> 'index.php?uri=/temp/cache/temp/test.html'
 (3) split uri=index.php?uri=/temp/cache/temp/test.html -> uri=index.php, args=uri=/temp/cache/temp/test.html
 (3) [perdir /hdd2/www/temp/] add per-dir prefix: index.php -> /hdd2/www/temp/index.php
 (2) [perdir /hdd2/www/temp/] trying to replace prefix /hdd2/www/temp/ with /temp
 (5) strip matching prefix: /hdd2/www/temp/index.php -> index.php
 (4) add subst prefix: index.php -> /temp/index.php
 (1) [perdir /hdd2/www/temp/] internal redirect with /temp/index.php [INTERNAL REDIRECT]
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/index.php -> index.php
 (3) [perdir /hdd2/www/temp/] applying pattern '.?' to uri 'index.php'
 (4) [perdir /hdd2/www/temp/] RewriteCond: input='/hdd2/www/temp/cache/temp/index.php' pattern='-f' => not-matched
 (3) [perdir /hdd2/www/temp/] strip per-dir prefix: /hdd2/www/temp/index.php -> index.php
 (3) [perdir /hdd2/www/temp/] applying pattern 'test.html' to uri 'index.php'
 (1) [perdir /hdd2/www/temp/] pass through /hdd2/www/temp/index.php

Which still isn’t working, but at least that “-f” flag condition is matching now with the full path.

However, now that I think about that and comment out that last RewriteRule I was used for testing, it works like a charm. Well, at least in this test it’s working, I still need to see about my stylesheets/etc out of the sandbox. :slight_smile:

This RewriteRule limiting to HTML files seems to do the trick.

RewriteRule \\.html$ /temp/cache%{REQUEST_URI} [L]

Also turning the RewriteEngine off in my cache & style folders to cut down on the number of rewrite actions.

That RewriteLog sure does come in handy. :smiley:

jb,

RewriteBase? hdd2/temp? Somewhere, this train’s left the track! The only thing shown (other than the logs - smart move!) is

RewriteRule \\.html$ /temp/cache%{REQUEST_URI} [L]

Regards,

DK

I have a tendancy to make a mess on my test box, a lot of times I leave stuff in that doesn’t belong & just keep a mental note for when I get it going live.

Here’s the [generic-ized] htaccess for the live site where the application is running in the DocumentRoot.

Options +FollowSymLinks -MultiViews -Indexes
AddDefaultCharset utf-8

ErrorDocument 403 "<title>Access Denied</title>Access Denied

<Files "config.php">
	Order Allow,Deny
	Deny from All
</Files>
<Files "common.php">
	Order Allow,Deny
	Deny from All
</Files>
<Files "php.ini">
	Order Allow,Deny
	Deny from All
</Files>

RewriteEngine on
RewriteBase /

## www
RewriteCond %{HTTP_HOST} ^domain
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]

##index
RewriteRule ^/*$ index.php [R=301]

## cache
RewriteCond /full-path-to-DocumentRoot/html_cache%{REQUEST_URI} -f
RewriteRule \\.html$ /html_cache%{REQUEST_URI} [L]

## category and page links
RewriteRule ^([a-z0-9\\-]*-wallpapers)-([0-9]*)\\.html$ index.php?cat=$1&page=$2 [L]

## old converter demo to new converter
RewriteRule ^converter-script-demo.* converter.php?from_demo=yes [QSA,R=301]

In the htaccess files for my /cache & /style directories I turn the RewriteEngine off.

I had the application print a <!–NEW–> comment at the beginning of the output when it was being displayed immediately after generation, but not saving that comment to the cache file.

Everything appears to be working live at the moment. :slight_smile:

jb,

It should NOT be working - not as well as you think, anyway!

Options +FollowSymLinks -MultiViews -Indexes
AddDefaultCharset utf-8

ErrorDocument 403 "<title>Access Denied</title>Access Denied[COLOR="Red"]"[/COLOR]

<Files "config.php">
	Order Allow,Deny
	Deny from All
</Files>
<Files "common.php">
	Order Allow,Deny
	Deny from All
</Files>
<Files "php.ini">
	Order Allow,Deny
	Deny from All
</Files>

RewriteEngine on
[COLOR="Red"]RewriteBase /[/COLOR]
[COLOR="Red"]# ONLY use if you have something from mod_alias to correct[/COLOR]

## www
RewriteCond %{HTTP_HOST} ^domain
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301[COLOR="Red"],L[/COLOR]]
[COLOR="Red"]# Which version of Apache?

# You wouldn't forget the ; at the end
# of a PHP statement would you?
# Without the Last flag, it gets ANDed with ...
[/COLOR]


##index
RewriteRule ^/[COLOR="Red"]?[/COLOR]$ index.php [R=301[COLOR="Red"],L[/COLOR]]
# WHY * instead of ?  How many leading ////'s do you expect?
# Again, which version of Apache?  Apache 2 insists on NO leading /'s
# Without the Last flag, ANDed with the following ...

## cache
RewriteCond [COLOR="Red"]/[/COLOR]full-[COLOR="Red"]web-[/COLOR]path-to-DocumentRoot/html_cache%{REQUEST_URI} -f
RewriteRule \\.html$ /html_cache%{REQUEST_URI} [L]
[COLOR="Red"]# The version of Apache makes a difference with the leading /
# This will ONLY work for a subdirectory (no start anchor) OR
# with Apache 1.x![/COLOR]

## category and page links
RewriteRule ^([[COLOR="Red"]-[/COLOR]a-z0-9][COLOR="Red"]+[/COLOR]-wallpapers)-([0-9][COLOR="Red"]+[/COLOR])\\.html$ index.php?cat=$1&page=$2 [L]
[COLOR="Red"]# This is Apache 2.x ONLY
# Use + (ONE or more) rather than * (ZERO or more)![/COLOR]

## old converter demo to new converter
RewriteRule ^converter-script-demo.* converter.php?from_demo=yes [QSA,R=301]

Sorry to burst your bubble but there are significant problems! Ask if you don’t understand my embedded comments.

Regards,

DK

I’m curious about a few things.

  1. What should I have in mod_alias for the RewriteBase ?
  2. I was under the impression that a R=xxx flag sent the request back for immediate external redirection, I placed the L flags to remind me. I guess I need to re-examine my rewrite log for that so I can fully understand how it’s doing it. Thanks!
  3. > “WHY * instead of ? How many leading ////'s do you expect?”

    Same reason I used * over + in the following rule.
  4. > # This is Apache 2.x ONLY
    # Use + (ONE or more) rather than * (ZERO or more)!

    I’m positive that it’s working as expected on Apache 1.3.33 CGI and 2.2.8, is there some sort of silent side-effect I may not see since I’m unable to use a RewriteLog on the 1.x server ?

    I’m fairly versed in Perl-compatible & JS regex. I’m not sure why I always think mod_rewrite only supports * rather than +, which is why I tend to use the former.
    I was thinking that it woud be fine since the application code will catch it, but now that I think more about it there could end up being multiple URLs pointing to the first page if I let the application catch it.

jb,

Yeah, yeah, yeah, so I AM pedantic :eye:. I used to work launching $B spacecraft across the solar system. Little things (like a change in sign in a guidance equation) have cost $M and years of man-hours to find after a lost mission so I do look into the details and I do know specifications (and how to use them properly).

Regards,

DK

What does any of that have to do with mod_alias ?

jb,

The answers to your mod_rewrite questions were indented in your quote.

The trailer was about specifying exactly what you want mod_rewrite to do THEN coding it.

Regards,

DK

Ah ok. It was highlighted with a different color last time, since the quote was all the same color this time I assumed it was just a quote. Thanks, and I apologise if I was rude. Consistency, consistency, consistency. :stuck_out_tongue:

The important thing is that it’s working now, & I believe the main issue with why it wasn’t working in the beginning had to do with me not using a full path in the RewriteCond that uses the -f flag.

Though, not I am a tad confused & I’m actually not 100% sure that was the issue. :?

jb,

Thank you.

I believe the reason for the initial problem was the question of Apache 1.x vs Apache 2.x, specifically within RewriteCond cache/%{REQUEST_URI} -f (NO match if Apache 1.x). As I’d said, the rest was okay (until you showed the whole lot!).

If you’re still confused, let me know where and we can get it straightened out (I tried within the indented sections - perhaps I should have italicized them?). The important thing here is not that it’s working but that you understand why it is!

Regards,

DK