Forum Moderators: phranque

Message Too Old, No Replies

more mod rewrite problems.

mod_rewrite giving me an ulcer

         

gilmanhunt

9:41 pm on Nov 13, 2009 (gmt 0)

10+ Year Member



I've been fighting with this one for about three hours. I'm modifying a very crufty .htaccess file with a bunch of specific mod-rewrite rules, but even when i use fake urls and stick it in the top of the file, it doesn't work.

Here's my instructions.... "If a user goes to this page, and they do NOT have this cookie set, and this file DOES exist, then give them this file (the one that does exist) without redirecting them.

Here's my (not working) .htaccess snippet:


# yes, original-dir/ONE Char/Many Chars/
RewriteCond %{HTTP_COOKIE} !^.*COOKIENAME.*$ [NC]
RewriteCond %{REQUEST_URI} ^/?original-request-dir/([^/])/(.*)/?$
# don't know if this actually works but need to save those vars - haven't gotten to this part of the problem yet.
RewriteCond -s cache/file/($1)/($2)/index.html
RewriteRule .* cache/file/$1/$2/index.html [L]

I have tried a full path to the file in the third line, I have tried using no variables and using paths I know exist, I have tried cutting back on the number of RewriteConds -- but each time I have more than one RewriteCond it fails with


File does not exist: %{DOCUMENT_ROOT}/original-dir

Thanks in Advance!

gilmanhunt

9:53 pm on Nov 13, 2009 (gmt 0)

10+ Year Member



hmm, I think I have it.

First, I had the wrong variable in the third line: needed %1 and %2. I also took out the parantheses.

I changed that last rewriteRule to specify the same path as the RewriteCond $2 and used the $1/$2 in the rewrite

jdMorgan

11:26 pm on Nov 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also don't need "^.*" and ".*$" on your cookie test... That's completely unnecessary, and wastes CPU time.

You can move the REQUEST_URI pattern to the RewriteRule for much faster execution (remove the leading slash, though). RewriteConds are not processed at all unless the RewriteRule pattern matches. Back-references to the URL-path-parts will also need to be updated to use $1/$2 instead of %1/%2.

Jim

gilmanhunt

6:43 pm on Nov 14, 2009 (gmt 0)

10+ Year Member



Hmm. Thanks Jim.
Apparently, it needs to handle whatever level of file; any number of dirs from A/Aardvark/African/ . The cache tree matches the web tree, so I should just be able to check for /original-request-dir/(.+)$ and match that against cache/file/%1 and if that file exists (that directory exists with an index.html file) then show that static page rather than the php one.

So this should work?


RewriteCond %{HTTP_COOKIE} !cookiename
RewriteCond cache/file/11/index.html -s
RewriteRule ^/?request-directory/(.*)$ cache/file/$1/index.html [L]

gilmanhunt

6:44 pm on Nov 14, 2009 (gmt 0)

10+ Year Member



ps. I need it to handle both african/$ and african$ ... so I have 2 blocks, the first one has the trailing slash.

gilmanhunt

7:02 pm on Nov 14, 2009 (gmt 0)

10+ Year Member



this rule shouldn't be causing a 301 moved permanently, should it?

jdMorgan

12:50 am on Nov 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_COOKIE} !cookiename
RewriteCond %{DOCUMENT_ROOT}/cache/file/$1/index.html -s
RewriteRule ^request-directory/(.*)/?$ cache/file/$1/index.html [L]

This is an internal rewrite, not a redirect. So no, it should not cause a 301 response.

Jim

jdMorgan

12:04 am on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please note the code corrections/modifications in the post above.

Jim

gilmanhunt

12:19 am on Nov 17, 2009 (gmt 0)

10+ Year Member



Nevermind. Thank you Jim :) I finally solved it with a (foo) - [pt] rule.

jdMorgan

1:03 am on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, so the output of another rule was being fed to this one, or vice-versa...

If possible, try to avoid 'stacked' rewrites or redirects, and use the [L] flag on all rules, unless you can't re-code to avoid it.

And also, inspect the current 'chain' of steps and see if you can find where that double-slash is being injected... That's nasty, whether you use [PT] or not...

Jim