Welcome to WebmasterWorld Guest from 54.204.100.232

Forum Moderators: bakedjake

Message Too Old, No Replies

mod_rewrite: my rules work... but why?

if I redirect, does it parse the rule-set a second time?

   
3:35 am on May 25, 2003 (gmt 0)

10+ Year Member



I'm disguising one of my dynamic pages by redirecting a static page to it. It works, but so do my bad-UA rules. Why? Shouldn't it redirect, see the last-rule flag, and exit without parsing the UA rules? Or does it parse the whole thing a second time?

In other words, if I request /foo/misc.html with a UA of "almaden" I get a 403 forbidden. I'm glad it works the way I want it to, I'd just like to understand WHY. :)

RewriteRule /foo/misc.html$ /foo/?section=misc [L]
RewriteCond %{HTTP_USER_AGENT} almaden [OR]
RewriteCond %{HTTP_USER_AGENT} ^ASPseek [OR]
RewriteCond %{HTTP_USER_AGENT} ^b2w [OR]
RewriteCond %{HTTP_USER_AGENT} ^BDFetch
RewriteRule ^.* - [F,L]

3:53 am on May 25, 2003 (gmt 0)

10+ Year Member



As per Apache mod_rewrite doc [httpd.apache.org]

Apache will process every RewriteRule. So what is happening in your case is that your first RewriteRule succeeds. Then apache passes on the newly rewritten url to the next RewriteRule directive and so on until the end.

So your url get's rewritten. But then your UA test matches and the "F" flage forces the forbidden.

daisho.

4:15 am on May 25, 2003 (gmt 0)

10+ Year Member



Hmm... I read a couple of documentation pages (including that one) before posting, but they didn't specifically talk about "last."

So, you are saying that if the URL is rewritten, it ignores the [L] directive and continues to parse the rules?

4:40 am on May 25, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Finder,

Your .htaccess file is processed for every incoming request that your server sees. That is, it is processed for every page, every css file, every external .js script, every .gif image, etc. that a browser requests.

The [L] flag only works in the context of a single request, and says, "If this rules matches, rewriting is done for this request.

An external redirect results in another request, so you'll run through everything in .htaccess again.

HTH,
Jim

5:05 am on May 25, 2003 (gmt 0)

10+ Year Member



Ahh, okay. Thanks Jim. :) Since the URL changes, apache runs it through httpd.conf a second time. That's what I figured, but I wanted to make sure.

mod_rewrite is too dangerous a tool to use haphazardly!

5:20 am on May 25, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Finder,

It is not simply that the URL changes. A 301 or 302 redirect requires communication with the browser. Instead of serving the page/resource requested by the browser, the server sends a 301 or 302 response (whichever you specify) back to the browser which says, "This resource has moved, use the following URL to re-request it."

Therefore, the browser "starts over" with the new address, and issues another request to your server.

HTTP is a "stateless" protocol, and each request exists completely independently of any that precede or follow it. So that is why every request runs through your .htaccess code; It can't be any other way, unless you get really tricky with server-side coding.

Another thing that helps sort this stuff out is to remember that the browser requests, and the server responds; The server never sends anything unless it is in response to a request from the browser (or spider, or any other user-agent... just using the term "browser" for simplicity's sake here).

HTH,
Jim

5:06 pm on May 29, 2003 (gmt 0)

10+ Year Member



Hmm.. then maybe I am misunderstanding the process. The browser gets a 200 (not a 301) when it requests /foo/misc.html. My logs show only that one request.

The only time I ever see a 301 is when a browser requests www.mysite.com/dir instead of www.mysite.com/dir/