views:

195

answers:

2

I have old.htm and new.htm on a directory, and I need to load file.php every time someone access old.htm, and load old.htm every time some access new.htm.

Here are my non-working rules:

RewriteRule ^old\.htm$ file.php?%{REQUEST_URI} [L]
RewriteRule ^new\.htm$ old.htm [L]

When old.htm is accessed, file.php is called. When new.htm is accessed, old.htm is "accessed" again, rewriting it to file.php. So, in the end, accessing new.php loads file.php, but I want to stop at old.htm.

Any ideas?

Here's RewriteLogLevel 3 output if it helps:

old.htm

[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/old.htm -> old.htm
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^old\.htm$' to uri 'old.htm'
[rid#2006ee0/initial] (2) [per-dir D:/dev/www/estadao/arquivoweb/] rewrite old.htm -> file.php?/arquivoweb/old.htm
[rid#2006ee0/initial] (3) split uri=file.php?/arquivoweb/old.htm -> uri=file.php, args=/arquivoweb/old.htm
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] add per-dir prefix: file.php -> D:/dev/www/estadao/arquivoweb/file.php
[rid#2006ee0/initial] (2) [per-dir D:/dev/www/estadao/arquivoweb/] strip document_root prefix: D:/dev/www/estadao/arquivoweb/file.php -> /arquivoweb/file.php
[rid#2006ee0/initial] (1) [per-dir D:/dev/www/estadao/arquivoweb/] internal redirect with /arquivoweb/file.php [INTERNAL REDIRECT]
[rid#200fb20/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/file.php -> file.php
[rid#200fb20/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^old\.htm$' to uri 'file.php'
[rid#200fb20/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/file.php -> file.php
[rid#200fb20/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^new\.htm$' to uri 'file.php'
[rid#200fb20/initial/redir#1] (1) [per-dir D:/dev/www/estadao/arquivoweb/] pass through D:/dev/www/estadao/arquivoweb/file.php

new.htm

[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/new.htm -> new.htm
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^old\.htm$' to uri 'new.htm'
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/new.htm -> new.htm
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^new\.htm$' to uri 'new.htm'
[rid#2006ee0/initial] (2) [per-dir D:/dev/www/estadao/arquivoweb/] rewrite new.htm -> old.htm
[rid#2006ee0/initial] (3) [per-dir D:/dev/www/estadao/arquivoweb/] add per-dir prefix: old.htm -> D:/dev/www/estadao/arquivoweb/old.htm
[rid#2006ee0/initial] (2) [per-dir D:/dev/www/estadao/arquivoweb/] strip document_root prefix: D:/dev/www/estadao/arquivoweb/old.htm -> /arquivoweb/old.htm
[rid#2006ee0/initial] (1) [per-dir D:/dev/www/estadao/arquivoweb/] internal redirect with /arquivoweb/old.htm [INTERNAL REDIRECT]
[rid#200fa90/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/old.htm -> old.htm
[rid#200fa90/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^old\.htm$' to uri 'old.htm'
[rid#200fa90/initial/redir#1] (2) [per-dir D:/dev/www/estadao/arquivoweb/] rewrite old.htm -> file.php?/arquivoweb/old.htm
[rid#200fa90/initial/redir#1] (3) split uri=file.php?/arquivoweb/old.htm -> uri=file.php, args=/arquivoweb/old.htm
[rid#200fa90/initial/redir#1] (3) [per-dir D:/dev/www/estadao/arquivoweb/] add per-dir prefix: file.php -> D:/dev/www/estadao/arquivoweb/file.php
[rid#200fa90/initial/redir#1] (2) [per-dir D:/dev/www/estadao/arquivoweb/] strip document_root prefix: D:/dev/www/estadao/arquivoweb/file.php -> /arquivoweb/file.php
[rid#200fa90/initial/redir#1] (1) [per-dir D:/dev/www/estadao/arquivoweb/] internal redirect with /arquivoweb/file.php [INTERNAL REDIRECT]
[rid#20e6c70/initial/redir#2] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/file.php -> file.php
[rid#20e6c70/initial/redir#2] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^old\.htm$' to uri 'file.php'
[rid#20e6c70/initial/redir#2] (3) [per-dir D:/dev/www/estadao/arquivoweb/] strip per-dir prefix: D:/dev/www/estadao/arquivoweb/file.php -> file.php
[rid#20e6c70/initial/redir#2] (3) [per-dir D:/dev/www/estadao/arquivoweb/] applying pattern '^new\.htm$' to uri 'file.php'
[rid#20e6c70/initial/redir#2] (1) [per-dir D:/dev/www/estadao/arquivoweb/] pass through D:/dev/www/estadao/arquivoweb/file.php
A: 

Not sure what's gonna happen with new.htm since the log seems only to cover the case of old.htm. But, try adding the NS flag to the old.htm rewrite rule to prevent it firing on the subrequest after the PHP script has been introduced.

jbourque
Hi jbourque, I separated the log to show what's from old.htm and what's from new.htm. I've tried adding `NS` in both lines and couldn't make it work :( I'm think the error happens on `internal redirect with /arquivoweb/old.htm [INTERNAL REDIRECT]` which should be an internal rewrite, maybe? Anyway, thanks :)
inerte
Sorry, I think I'm stumped as far as the rewrite rules are concerned. Can you address it with a redirect, rather than a rewrite? Or can you fix it at the file system level, make a symbolic link from new.htm to old.htm rather than rewriting the URI?
jbourque
Hi jbourque, the actual rewrite rules that I'm using are a little more complicated than the reduced example on my original question, which is something that I tested, doesn't work, but it's the simplest form which emulates what I'm trying to do. There are dozens of thousands of files, with hundreds of thousands of visitors a day :) I'm trying to avoid redirects or opening the file with php, and just sticking with the rewrite.
inerte
A: 

Try to additionally check the original requested URL instead of just the current URL:

RewriteCond %{THE_REQUEST} ^GET\ /old\.html
RewriteRule ^old\.htm$ file.php?%{REQUEST_URI} [L]
RewriteRule ^new\.htm$ old.htm [L]
Gumbo
After a little hacking on the regex I got it working, thanks :) There's nothing wrong with your answer, by the way! I just had to get all the characters right on `^GET\ /arquivoweb/[a-z]+/[\d]{4}/[a-z]+/not[\d]{8}p[\d]+\.htm`, but it works!
inerte