Forum Moderators: phranque

Message Too Old, No Replies

Having trouble with pattern matching

         

dfresh4130

4:55 pm on May 5, 2009 (gmt 0)

10+ Year Member



I'm currently having an issue trying to get a pattern match for a redirect to work and stop processing. I need to get the following URI to get redirected to a static one:

/redirect?request=multimedia

Below is an example of a rule I'm testing with, but it just doesn't seem to be working. I've tried putting a backslash before the ? in the pattern match part and using the hex version of %3F as well. When I test I see in the access logs that it's giving a 502 error after a minute. Also enabled the rewrite log and I've included that below. It seems that it just keeps processing the rules because it's not properly pattern matching. Any help on this is much appreciated.

RewriteRule ^/redirect?request=multimedia [11.11.11.11...] [P,L,NE]

11.11.11.11 - - [04/May/2009:17:15:49 --0500] [11.11.11.11/sid#3001e920][rid#6089a298/initial] (2) init rewrite engine with requested uri /redirect
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^/redirect.?request=multimedia' to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^/PA_1_ICGAQI420GTJ502LD4J3311GQ1/redirect/redirect(.*)'
to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^/PA_1_ICGAQI420GTJ502LD4J3311GQ1/DownloadContent(.*)' to
uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^/redirectredirect(.*)' to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '.?' to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (4) RewriteCond: input='/redirect' pattern='^/ABCD/PA_1_BC1BQI42000K802N9R3F4
Q3000/redirect(.*)' => not-matched
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^.*$' to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (4) RewriteCond: input='' pattern='^[^:]*:([^;]+)' => not-matched
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (3) applying pattern '^/DRM(.*)' to uri '/redirect'
11.11.73.254 - - [04/May/2009:17:15:49 --0500] [11.11.72.30/sid#3001e920][rid#6089a298/initial] (1) pass through /redirect

Caterham

5:30 pm on May 5, 2009 (gmt 0)

10+ Year Member



As you can see from

applying pattern '^/redirect.?request=multimedia' to uri '/redirect'

the query string is not part of the string matching against your regular expression. You'll have to use a condition in order to check the query string (rewriteCond %{QUERY_STRING} pattern).

dfresh4130

7:08 pm on May 5, 2009 (gmt 0)

10+ Year Member



Ok, so I need to use the RewriteCond QUERY_STRING option, but I'm confused as to what the pattern I put in should be. Should I put in the entire URI or just a part of the URI? Also what would the beginning of the RewriteRule be on the next line to use this RewriteCond? I can't seem to find any good examples for QUERY_STRING.

g1smd

11:32 pm on May 5, 2009 (gmt 0)

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



The Rewrite Rule pattern looks only at the 'localised' folder path and file name (not the domain name, nor the query string).

Query String looks at only the Query String part. You'll likely use a pattern like

&?param=value&?
here.

dfresh4130

3:57 am on May 6, 2009 (gmt 0)

10+ Year Member



I keep coming up with blanks on this one. So far this is what I have, but I can't seem to get it to match properly. Maybe it's just a syntax issue, but I can't figure it out. Any ideas?

RewriteCond %{QUERY_STRING} \?request\=multimedia
RewriteRule ^/redirect%1 [aaa.aaa.com...] [L,NE]

jdMorgan

5:32 am on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First get the rule working, then tweak.

RewriteCond %{QUERY_STRING} &?request=multimedia&?
RewriteRule ^/redirect$ http://example.com/ABCD/PA_1_ICGAQI420GTJ502LD4J3311GQ1/MediaChannel? [R=301,L]

This redirects client requests for /redirect?request=multimedia to http://example.com/ABCD/PA_1_ICGAQI420GTJ502LD4J3311GQ1/MediaChannel

Because you said you wanted a "static URL" in your first post, I assumed that you want to drop the query string. If not, just take the question mark off the RewriteRule substitution URL.

The query string test will accept a query of "request=multimedia" or "foo=bar&request=multimedia" or "request=multimedia&foo=bar" or "quux=foo&request=multimedia&bar=closed" or any other query string containing "request=multimedia", as long as "request=multimedia" is properly bounded by ampersands if any other name/value pairs are present.

You may think of the "&?" subpatterns as "soft anchors." That is, if *any* character precedes or follows "request=multimedia", then it must be an ampersand. This prevents matches on, for example, "newrequest=multimedia" or "request=multimediareviews".

Jim

[edited by: jdMorgan at 5:34 am (utc) on May 6, 2009]

dfresh4130

3:19 pm on May 6, 2009 (gmt 0)

10+ Year Member



Ok, this definitely works. I can now successfully get it to redirect. However, is it possible to have it only match the /redirect?request=multimedia exactly? For example have it catch http://www.example.com/redirect?request=multimedia and not catch http://www.example.com/redirect?request=multimediaU?

jdMorgan

3:35 pm on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Research into regular-expressions pattern-matching is highly-recommended, and specifically in this case, the concept of "anchoring." See the resources cited in our Apache Forum Charter [webmasterworld.com].

RewriteCond %{QUERY_STRING} [b]^r[/b]equest=multimedi[b]a$[/b]

Be aware that a single typo in mod_rewrite can take down your server -- if you are lucky. If you are unlucky, its effects may be subtle, possibly destroying your search engine ranking slowly over time. Exact requirements specifications, attention to coding detail, and very thorough testing are demanded.

Jim

g1smd

7:17 pm on May 6, 2009 (gmt 0)

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



If the alternative URL request is NOT going to be redirected, you need to be sure that whatever the server does return for that request is exactly what you want it to be.

It is likely that you would like a 404 status in the HTTP header, and it is even more likely that it actually returns a blank template page with a 200 OK status code at present.