Forum Moderators: phranque
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
>>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
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.
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]
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
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.
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
RonPK, your question was a good one, while my answer was not. For that, I apologize.
Jim
>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