Forum Moderators: phranque

Message Too Old, No Replies

mod write how to pass parameters after 'URL?'

mod_write how to pass the parameters after a '?' on the left side of a url.

         

wmmyg

7:09 pm on Jul 20, 2006 (gmt 0)

10+ Year Member



I've had some success with basic mod_write stuff but this one has me stumped.

I need this path:
www.mydomain.com/forums/profile.php?6,999,session=a876adefffbcy
to map to
www.mydoman.com/cgi-bin/showprofile.cgi?999

I tried experimenting with rewritecond %{query_string} as a way of trying to pick you the data after the question mark but none of the examples I could find seemed to apply.

Any help would be very much appreciated...:)

William

jdMorgan

2:45 am on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I tried experimenting with rewritecond %{query_string}

Creating a back-reference with RewriteCond %{QUERY_STRING} and de-referencing it in the rewriterule by using "%1" is the correct approach. Please post one of your 'experiment' rules so we can discuss it.

JIm

wmmyg

8:18 am on Jul 21, 2006 (gmt 0)

10+ Year Member



>> I tried experimenting with rewritecond %{query_string}

>>Creating a back-reference with RewriteCond %{QUERY_STRING} and de-referencing it in the rewriterule by using "%1" is the correct approach. Please post one of your 'experiment' rules so we can discuss it.

Great :) Many thanks for the offer.

Actually, I was following one of your solutions to an earlier topic and trying to alter it to suit my situation:

Problem:
>>http://www.example.com/guestbook/screen.php?vgbxiferp=&vgbreliam=55 to >>http://www.example.com/guestbook/screen.php

Solution:
>rewritecond %{query_string} vgbxiferp=&vgbreliam
>RewriteRule ^guestbook/screen\.php$ >http://www.example.com/guestbook/screen.php? [R=301,L]

I can see that 'vgbxiferp=&vgbreliam' matches part of the query string, but I have not managed to figure out what else it does. Basically I'm having trouble making sense of the apache docs about rewritecond %{query_string}) and I don't understand how it works - not yet anyway....:)

One of my failed attempts (does not redirect)

RewriteEngine On
rewritecond %{query_string} profile
RewriteRule ^profile.php?([0-9]+)$ ../cgi-bin/oshowprofile.cgi?pseq=$1 [NC,L]

using the URL [mydomain.com...] gives 'The requested URL /profile.php was not found on this server' (as profile.php does not actually exist).

By the way, this is all in .htaccess

Any advice would be very much appreciated.

William

JIm

RonPK

10:26 am on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine On 
rewritecond %{query_string} profile
RewriteRule ^profile.php?([0-9]+)$ ../cgi-bin/oshowprofile.cgi?pseq=$1 [NC,L]

About

%{query_string}
: it is common and usually best to use upper case when referring to server environmental variables.

QUERY_STRING is the part of the URL that comes after the question mark (?), and before the hash (#), if any. If you're trying to match the name of the file (profile.php), use REQUEST_URI (which happens to include the query string as well).

About

../cgi-bin/oshowprofile.cgi?pseq=$1
: I'm not sure but I think you can't use relative paths in substitutions.

jdMorgan

1:34 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of a URL. They are data attached to a URL, to be passed to the resource at that URL. Therefore, query strings are handled separately in mod_rewrite and many scripting languages.

I need this path:
www.mydomain.com/forums/profile.php?6,999,session=a876adefffbcy
to map to
www.mydoman.com/cgi-bin/showprofile.cgi?999


RewriteCond %{QUERY_STRING} ^([0-9]+,)?([0-9]+)
RewriteRule ^forums/profile\.php$ /cgi-bin/showprofile.cgi?%2 [L]

Note that as in your example, the first digit (6) is dropped.

The back-reference %2 is used in the RewriteRule to 'copy' the userID number (999 in your example) in the second parenthesized regular-expressions pattern into the new URL-path.

I assumed that the 6,999 was the userID number and that you want to drop the first digit if it is present. There may be more efficient ways of coding this if the "6" is part of a different parameter or if it is always present. The code above will work for your example, but perhaps not fit your real needs exactly. That's the nature of mod_rewrite: it requires precision in requirements specification and implementation.

RonPK's note about character-case is a good one. When coding server-side stuff, it's best to conforam *exactly* to case conventions as documented by Apache. This will often prevent problems when using mixed *nix and Windows OS platforms for Apache testing -- In many circumstances, Windows is case-insensitive, whereas *nix is case-sensitive. *Do not* feel free to use 'personal styles' when coding using server modules -- These modules generally use simple parsers which do not allow the kind of flexibility you find in scripting languages like PERL and PHP.

Anyway, the key to this current project is the proper use of the RewriteCond %{QUERY_STRING} to create a back-reference to part of the query data, and re-use of that data in the substitution URL in the RewriteRule. A bit of research [httpd.apache.org] into those terms will likely clarify any remaining questions.

JIm

RonPK

6:05 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of a URL.

Are you sure? The relevant RFCs seem to disagree, but that could be due to me missinterpreting the difference between URI and URL.

RFC3986 (Uniform Resource Identifier (URI): Generic Syntax): "The generic URI syntax consists of a hierarchical sequence of components referred to as the scheme, authority, path, query, and fragment."

Its predecessor, RFC2396:

This "generic URI" syntax consists of a sequence of four main components: 
<scheme>://<authority><path>?<query>
each of which, except <scheme>, may be absent from a particular URI.

jdMorgan

8:31 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A URL refers to a resource, not a 'page,' a file, or anything else. A script is a resource, while the data to be passed to it is not.

Sloppy language --and there's plenty of it on the Web-- leads to sloppy thinking and confusion, as demonstrated here. A query string is not part of a URL, it is data attached to a URL to be passed to the resource at that URL, period, full stop.

Jim

jdMorgan

1:01 am on Jul 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry if the above sounds rude. Sometimes when checking in here, I'm doing so in a hurry. So I tend to write what comes to mind, often tersely, and often without sufficient 'editing' in my head. There are many inconsistencies in Web documentation, everything from the misspelling of HTTP_REFERER to the contradictory documentation of URL and URI spaces. These things irritate me because they make my 'job' here much harder, and in this case, I let that irritation show through.

RonPK, your question was a good one, while my answer was not. For that, I apologize.

Jim

RonPK

8:33 am on Jul 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, no problem.

wmmyg

2:46 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



>>I need this path:
>>www.mydomain.com/forums/profile.php?6,999,session=a876adefffbcy
>>to map to
>>www.mydoman.com/cgi-bin/showprofile.cgi?999

>RewriteCond %{QUERY_STRING} ^([0-9]+,)?([0-9]+)
>RewriteRule ^forums/profile\.php$ /cgi-bin/showprofile.cgi?%2 [L]

Sorry for the delay.

Many thanks indeed Jim. I was just not getting the basic principles of using for RewriteCond %{QUERY_STRING}, but I get it now thanks to your clear and considered explanation.

Thanks too to Ron for his helpful comment about uppercase on QUERY_STRING.

To both of you: May Google index all your pages, may your ranking always be high and may Alexa shine on all your sites...:)

Cheers,

William