For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > March 2005 > Query string to URL?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Query string to URL?
Jan Eden

2005-03-07, 3:55 pm

Hi,

I need to translate a URL with a POST query string into a URL like this:

http://mysite.com/pages?id=3D1234
should become
http://mysite.com/pages/1234

I currently use mod_rewrite to turn the initial URL into

http://mysite.com/redirect.pl?id=3D1234

and redirect.pl redirects to

http://mysite.com/pages/1234

Since mod_rewrite cannot work on query strings itself, I cannot use it to m=
ake the transition smoother - but do I really need redirect.pl?

Thanks,

Jan
--=20
A common mistake that people make when trying to design something completel=
y foolproof is to underestimate the ingenuity of complete fools.
Thomas Bätzler

2005-03-07, 3:55 pm

> I need to translate a URL with a POST query string into a URL
> like this:
>
> http://mysite.com/pages?id=1234
> should become
> http://mysite.com/pages/1234


OTTOH:

RewriteRule ^/pages/([^/]+)/?$ /pages?id=$1 [QSA,NS,L]

Or even easier, leave your URL like that and parse the
PATH_INFO variable for your id.

HTH,
Thomas

Jan Eden

2005-03-07, 3:55 pm

Hi Thomas,

Thomas B=E4tzler wrote on 07.03.2005:

>
>OTTOH:
>
>RewriteRule ^/pages/([^/]+)/?$ /pages?id=3D$1 [QSA,NS,L]
>
>Or even easier, leave your URL like that and parse the PATH_INFO
>variable for your id.
>


Thanks. I already have this rule in place somewhere else, but I need to hav=
e something which works the other way around.

The rule you proposed (which I already use) does this:

http://mysite.com/pages/1234 -> http://mysite.com/pages?id=3D1234

But I need some rule to do this:

http://mysite.com/pages?id=3D1234 -> http://mysite.com/pages/1234

And since mod_rewrite does not parse the query string, I have a problem her=
e.

The background is the following: I have a dynamic site, for which I can do =
this:

http://mysite.com/pages?id=3D1234 -> http://mysite.com/cgi-bin/show.pl?id=
=3D1234

using a rule similar to the one you proposed.

But I need to create a static version of the site (for distribution on CD),=
which should work without Perl scripts involved. So I cannot do

http://mysite.com/pages?id=3D1234 -> http://mysite.com/cgi-bin/redirect.pl?=
id=3D1234

and have redirect.pl redirect to the /html/1234.html file. This needs to be=
done by Apache.

Thanks,

Jan
--=20
Common sense is what tells you that the world is flat.
Chris Devers

2005-03-07, 3:55 pm

On Mon, 7 Mar 2005, Jan Eden wrote:

> But I need some rule to do this:
>
> http://mysite.com/pages?id=1234 -> http://mysite.com/pages/1234
>
> And since mod_rewrite does not parse the query string, I have a
> problem here.


So use redirect instead of rewrite:

RedirectMatch /pages?id=(.*) http://mysite.com/pages/$1

Won't that work?



--
Chris Devers
Jan Eden

2005-03-08, 8:55 am

Hi Chris,

Chris Devers wrote on 07.03.2005:

>On Mon, 7 Mar 2005, Jan Eden wrote:
>
>
>So use redirect instead of rewrite:
>
> RedirectMatch /pages?id=3D(.*) http://mysite.com/pages/$1
>=20
>Won't that work?
>


Yes, it would. Doh! I was so focussed on mod_rewrite that I forgot about si=
mpler options.

Anyway, I finally figured something out which works with RewriteRules. My f=
irst attempt was this:

RewriteCond %{QUERY_STRING} ([0-9]+)
RewriteRule ^pages$ html/%1.html [L,NC]

which only works if the QUERY_STRING is part of the URL (i.e. I do a GET re=
quest)

This was a basis for the following construct:

RewriteCond %{QUERY_STRING} id=3D([0-9]+)
RewriteRule ^pages$ pages/%1? [R,NC]
RewriteRule ^pages/([0-9]+) html/$1.html [L,NC]

which gives me what I need, turning the URL with a query string into someth=
ing like pages/1234 and invisibly rewriting the resulting URL.

Thanks again for your help,

Jan
--=20
I'd never join any club that would have the likes of me as a member. - Grou=
cho Marx
Jan Eden

2005-03-08, 8:55 am

Chris Devers wrote on 07.03.2005:

>On Mon, 7 Mar 2005, Jan Eden wrote:
>
>
>So use redirect instead of rewrite:
>
> RedirectMatch /pages?id=3D(.*) http://mysite.com/pages/$1
>=20
>Won't that work?


Ah, now I remember why this won't work: I use several different hostnames f=
or my site, and I do not want them to have them changed when redirecting UR=
Ls.

Cheers,

Jan
--=20
Hanlon's Razor: Never attribute to malice that which can be adequately expl=
ained by stupidity.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com