Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

301 Redirect using Perl

by siva kumar (Pilgrim)
on Mar 26, 2007 at 14:22 UTC ( [id://606583]=perlquestion: print w/replies, xml ) Need Help??

siva kumar has asked for the wisdom of the Perl Monks concerning the following question:

How can I do 301 redirect using perl?
I was asked to do 301 redirect for a page. I have placed the following code in my cgi program.
#!/usr/bin/perl print "Status: 301 Moved Permanantly\n"; print "Location: http://www.mydomain.com/page.html\n\n"; exit;
But the result is "Status: 301 Moved Permanantly Location: http://www.mydomain.com/page.html" printed on my browser. My question is,
1. Whats the use of 301 redirect and how to do that? 2. Do I require any Apache setting OR .htaccess setting? 3. Why I can't simply do "Http-Equiv=refresh" or print "Location:http://www.mydomain.com/page.html\n\n" for the same task?
Thanks

Replies are listed 'Best First'.
Re: 301 Redirect using Perl
by ikegami (Patriarch) on Mar 26, 2007 at 15:58 UTC
    • First, what you have there should work for Apache.

      >perl -e "use CGI qw( redirect ); print(redirect(-uri => 'http://www.e +xample.com/page.html', -status => 301))" Status: 301 Location: http://www.example.com/page.html

      What server are you using? For some servers (e.g. IIS), you need to send an HTTP status line instead of the pseudo-HTTP "Status" header.

      >perl -e "use CGI qw( redirect ); print(redirect(-uri => 'http://www.e +xample.com/page.html', -status => 301, -nph => 1))" HTTP/1.0 301 Server: cmdline Status: 301 Date: Mon, 26 Mar 2007 15:59:21 GMT Location: http://www.example.com/page.html
    • Secondly, are you sure that's the script you executed? If it doesn't work, it should fail by displaying a 500 error, by displaying some other error or by displaying a blank page. It should not fail showing the HTTP header. In order to display what you said it displayed, it would have had to print a CGI header before the first print.

Re: 301 Redirect using Perl
by philcrow (Priest) on Mar 26, 2007 at 14:27 UTC
    The status number must go in the HTTP response, not just in a header. The easiest way is to use CGI and call its redirect method.

    Phil

Re: 301 Redirect using Perl
by sgifford (Prior) on Mar 26, 2007 at 16:05 UTC
    Your code looks OK to me, and indeed works on my Web server. Is that the entire script? And is it run as a standard CGI script (not mod_perl, nph, etc.)?

    Two useful debugging tools are to run the script by hand to see exactly what it's printing, and getting the URL where your script resides with a command-line tool, like wget or lwp-request from LWP.

    philcrow suggests that you use CGI's redirect method, but that actually just prints a Status and Location header, just as your script does. Still, this module is more likely to work under mod_perl and the like, and might be worth a try, if nothing else to verify that it's behavior is identical to your script.

Re: 301 Redirect using Perl
by jhourcle (Prior) on Mar 26, 2007 at 17:16 UTC
    1. Whats the use of 301 redirect and how to do that?

    It tells web browsers and search spiders that you've reorganized your site, and that the item they were looking for is at a new location, and they should stop looking at the old location.

    As for how to do it, the code you gave _should_ do what you're attempting to do, but the fact that it's displayed by the browser leads me to suspect that something else has already generated an HTTP header. You may need to contact your webserver administrator to see if they have some sort of a wrapper that you're running under.

    2. Do I require any Apache setting OR .htaccess setting?

    Not typically, but if the server is set up to wrap the CGIs, and insert HTTP headers, you're going to need to shut that off for this script. As it's not a typical thing for servers to do, and there's more than one way to implement wrappers, you're going to need to talk to the administrator of the webserver you're running this on.

    3. Why I can't simply do "Http-Equiv=refresh" or print "Location:http://www.mydomain.com/page.html\n\n" for the same task?

    Well, the two methods that you are asking about are similar to each other, but fundamentally different from a 301. Basically, if you do that, you're telling the web browser that the location right now is at some other place, but nothing about the URL that they had asked for -- so they're likely to come back again, and then be redirected.

    Meta refresh or a 302 request (which is typically assumed if you send a location and no status) should not be cached by proxies, and so any subsequent requests for that page should call the CGI every time they want the page, to find out if the redirection has ended or changed location.

    If you're trying to track entry points to the same resource, you might want this extra overhead. Personally, I use 302s as a convenience thing for a few URLs -- I have a 'latest' URI, which redirects the user to the most recent item. (although I could just serve it directly, we had a problem with people trying to share the 'lastest' URI, which would updated to a new item before they recipient followed the link). In this case, I _want_ the user to continue using the 'latest' URI to get the latest item, but I don't want them associating the URI with a specific item.

Re: 301 Redirect using Perl
by shigetsu (Hermit) on Mar 26, 2007 at 14:27 UTC
Re: 301 Redirect using Perl
by ww (Archbishop) on Mar 26, 2007 at 20:11 UTC
    re 2: I suspect the answer is 'yes.'

    That's an inference, based on the fact that hosting companies typically provide some sort of management panel that allows you to specify "custom" error pages... However, a search of Apache's config options may give you a direct route. Perhaps one or the other of these approaches will be useful, depending on whose server it is, etc....

    ... or, I may be all wet, but FWIW.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://606583]
Approved by philcrow
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found