How to rewrite URL with Perl redirecting?

Hello,
I always used the following code to redirect from a Perl script to another:

print ("Location: /path_to_destination_script/
");
print ("Content-type: text/html

");

and anytime I got the URL location rewritten in my browser.

Well, now I have moved to a new webserver and if I call the same script, the URL is not updated, it remains displaying the previous script. probably there is a different configuration in the httpd.conf of the Apache server or in the Perl configuration?

Any advice is very welcome. I need to have the URL rewritten.

Thank you in advance.

Sincerely,
Fabrizio Ferrari

Hello Fabrizio,
You should output only the location header:

print ("Location: /path_to_destination_script/

");

Thank you monolitik for your kind reply. I solved the issue by writing the full URL path to the destination script including the “http://”.

Thank you for your kindness.

Sincerely,
Fabrizio

> I solved the issue by writing the full URL path to the
> destination script including the “http://”.
I never used Perl but Yes,
in fact the Location should be an absolute URI as stated here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

The syntax of the absoluteURI can be found here:
http://www.ietf.org/rfc/rfc2396.txt

Using PHP will work even without the http part,
but that’s because, I think, PHP will add it automatically…

:slight_smile: Andrea

Thank you for your links Andrea, very interesting resources!

All the best,

Fabrizio.

I need to redirect URL including query string and fragment. Is this at all possible in Perl?

Need to redirect
mydomain.com/oldurl/view.php?page1#section2

to

mydomain.com/newurl/view.php?page1#section2

Whatever I do I loose the ‘#section2’ part of URL.

Hello.

The $ENV{“QUERY_STRING”} variable should contain the complete query.

All the best.

Fab.

QUERY_STRING contains the query until the #mark. Everything after # is truncated.

Well, anyway I 've beed digging around and it seems impossible to get the fragment part of URL in Perl because it 's not passed to server.

Yes, sorry for my confusion. I tried myself and actually everything over # (included the # sign) isn’t present in any ENV variable.

I am sorry.

Sincerel,
Fab.

Why do you people need $ENV{“QUERY_STRING”} anyway? I suppose you want to use #mark to tell the browser to go to a specific part of the page, no? Browser doesn’t need Perl’s %ENV hash it will look up for the #mark part of the url automatically when loading the page.

So you do something like


print "Location: [url="http://www.site.com/index.cgi?key=value#marknn"]http://www.site.com/index.cgi?key=value#mark\
\
"

and when the browser will display the resulting page, it will jump to #mark in it.

Great reply Eych! This is true.

Thank you.

Fabrizio.