Forum Moderators: phranque

Message Too Old, No Replies

Understanding {THE REQUEST}

         

CWebguy

8:19 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Ok, one last thing I want to cover to conquer this mountain.

Again, thank you guys for all your help and support.

In the request

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /dir/\?page=([0-9]+)\ HTTP/

So putting this into English, I get, "If there is a request from the browser (I'm not sure why capital letters A-Z here? I get the next part, just pattern matching what the page request is, except I don't understand why there is a backslash at the end, but not important) AND if it is an HTTP request (the HTTP/ part), then perform the next rule."

The only part I don't get looking at this logically is the ^[A-Z]+\ I could see doing it without it, but I don't understand what this is (especially considering since I don't have any capital letters in my URI).

Thanks a bunch!
CWebguy

[edited by: CWebguy at 8:24 pm (utc) on Mar. 11, 2009]

g1smd

8:30 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you use the Live HTTP Headers extension for Firefox you can see exactly what goes into THE_REQUEST.

It is exactly what your browser requests from the server as in:

GET /somepage.php?query=somevalue HTTP/1.1

So

[A-Z]{3,9}
matches the method, in this case the
GET
. In your case you have
[A-Z]+
which does the same job, but is not so efficient.

The

HTTP/
matches all versions of HTTP request, so that (hopefully) you won't have to change your code when HTTP/2.x arrives.

Literal spaces and literal question marks are escaped as already shown.

CWebguy

8:39 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



ahhh, ok. So instead of HTTP/* you can just do HTTP/ ?

And the backslash after the [A-Z] and pattern matching, what's that there for?

Mucho gracias

Note: I will try out that extension, thanks.
I've done some header stuff with PHP so I know what you are saying. thanks.

[edited by: CWebguy at 8:42 pm (utc) on Mar. 11, 2009]

g1smd

8:49 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** So instead of HTTP/* you can just do HTTP/ ? ***

No. Neither of those are valid constructs for matching the pattern here.

Did you mean .* instead of * here? That would work, but there is no need to put .* on the end of an unanchored pattern.

As for your HTTP/ ? pattern, that would match HTTP/ followed by a space followed by a question mark. That would never happen in a real request.


*** And the backslash after the [A-Z] and pattern matching, what's that there for? ***

Literal spaces and literal question marks are escaped with a preceding backslash as already shown.

[edited by: g1smd at 9:01 pm (utc) on Mar. 11, 2009]

CWebguy

8:57 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Oh, I meant the question mark to be for the sentence, sorry, haha.

Ok, so any backslash and then a space is just a space, gotcha ;)

Wow, facinating, haha. Thanks a bunch.

g1smd

9:03 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't get that the question mark was a question mark asking a question. I took it as being a part of your pattern. LOL. :)

But yes. For any pattern, code like

^pattern.*$
can almost always be simplified to
^pattern
but you will need the .* if you intend to capture the value as in
^pattern(.*)

jdMorgan

9:12 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd do well to study (not skim) the resources listed in our Forum Charter (see link at top left of every WebmasterWorld forum page); They will answer many questions about regular expressions and mod_rewrite itself. This stuff can't be "guessed at," and all of the minutiae certainly can't be picked up in forums -- The very few volunteer contributors here have limited time, and re-iterating what has already been documented in very publicly-accessible places in a poor use of their rime.

mod_rewrite is a very powerful tool, but as with any powerful tool, it is best to learn how to use it before relying on it or risking the (potentially severe) damage it can do in untrained hands. You can accomplish a lot with it and it's available at no extra charge on all competitive hosting accounts, but the "cost of admission" is several days (at least) in research time.

Jim