Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - query string

         

big_k105

4:13 pm on May 5, 2008 (gmt 0)

10+ Year Member



Here is my rules, Then I will get into what the actual problem is.

RewriteCond %{QUERY_STRING}^(.*)htmlInclude=(.*)&(.*)$ [OR]
RewriteCond %{QUERY_STRING}^(.*)htmlInclude=(.*)$
RewriteRule ^/path/to/filehttp://localhost/html/%2.html? [R=301,L]

url going in:
[localhost...]

url coming out:
[localhost...]

url I want:
[localhost...]

Ok, so first off my rule is removing the first part of the query string up until the htmlInclude part, but then after that it is only removing the last variable, but I want it to remove any variables that show up after the htmlInclude part. Even if there is just 1 which will work, or if there is as many as 5 or more. I have done a lot of looking and I don't get why this doesn't work, I assumed my first RewriteCond would take everything after the htmlInclude=htmlFile& and then I could just throw that away, but it only takes the last variable. Any pointers on what I could be doing wrong would be greatly appreciated, Thanks

jdMorgan

4:26 pm on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both RewriteConds will match, because you've used the greedy and promiscuous ".*" pattern. As a result, the second RewriteCond will pick up the entire 'tail' of the query string in %2. You should avoid using ".*" in regular-expressions patterns unless it is in fact the only pattern that will work. Here's a rewrite:

RewriteCond %{QUERY_STRING} &?htmlInclude=([^&]+)
RewriteRule ^/path/to/file http://localhost/html/%1.html? [R=301,L]

This uses an unanchored RewriteCond pattern. The pattern requires that if any character precedes "htmlInclude", it must be an ampersand. This is to prevent a problem, say, with using another variable named "shmtlInclude" in the future. It then picks up all characters following "htmlInclude=" up to but not including the next ampersand, if one is present. The rest of the query string is ignored, as is anything preceding "htmlInclude".

Again, the ".*" pattern is apparently-easy to use, but often causes undesired side effects. Avoid it and use specific patterns whenever possible.

Jim

big_k105

4:38 pm on May 5, 2008 (gmt 0)

10+ Year Member



Thanks for the help, That worked like a charm. I will have to go and look through and double check to see if I have ".*" any other place and replace them with a better pattern to make sure I don't get any unexpected results. Thanks again