Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Doing a proper 404 redirect

by DreamT (Pilgrim)
on Mar 23, 2010 at 15:00 UTC ( #830306=perlquestion: print w/ replies, xml ) Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Yo,
I've manage to make a proper 301 redirect with Perl through the following code:
my $new_url = "http://www.example.com/new_url.htm"; if ($do_the_redirect) { print "Status: 301 Moved Permanently\n"; print "Location: ${new_url}\n\n"; }
Is it possible to do this for a 404 also? I'm having the following in mind:

1. Give the search engines the correct info.
2a. Redirect the user to a custom page, or 2b. Redirect the customer to Apaches Default 404 page (if one exists?)

Comment on Doing a proper 404 redirect
Download Code
Re: Doing a proper 404 redirect
by JavaFan (Canon) on Mar 23, 2010 at 15:18 UTC
    Your question is not a Perl question.

    Having said that, a resource either doesn't exist (404), or has moved (301) (or something else). It cannot be moved and not exist. So, I don't understand what mean with wanting to handle a 404 as a 301.

      Well, it kinda is a Perl question, since I want to be able to "express" a 404 error using Perl.

      Regarding the 404, there was some wrong thinking on my part:) It's of course up to me to show relevant info when the 404 is made. But, I still want search engines and such to "get" that a 404 has been made.

      In other words, I want to write a correct 404 header. The information displayed after that is basically up to me.
        Wait. So, given that you demonstrated you know how to print "301", you now seek knowledge how to print "404"?

        Sorry, but that's way over my head.

Re: Doing a proper 404 redirect
by ikegami (Pope) on Mar 23, 2010 at 15:29 UTC

    You can't redirect (3xx) and signal a client error (4xx).

    I don't know what you mean by "giving the search engines the correct info.", so I can't suggest a solution.

Re: Doing a proper 404 redirect
by DStaal (Chaplain) on Mar 23, 2010 at 20:27 UTC

    Typically, if at all possible, you'd want to do this through Apache. (Which will, always, have a default 404 page. It may have one or more custom pages as well.)

    Doing it even via a CGI script is likely to cause problems with someone, as you aren't really putting it into the HTTP response header, but into the payload. It'll work sometimes, in some browsers, but I'd check to make sure you can't set up a real one first.

      Well, I'm building an architecture where Perl is supposed to handle 301:s and 404:s, opposed to handling "everything" via .htaccess (I don't like to write .htaccess dynamically)

      But maybe .htaccess is the only way to go? I know it's not Perl related but...Maybe RewriteMap is a possible solution?

        I think you have a problem understanding the HTTP protocol.

        Imagine you knock on a door and ask for a photo of a certain teddy bear. This is a HTTP request. Now, you could get some different responses:

        Human behind the doorStatus CodeStatus Text from RFC 2616Notes
        "OK, here you are", and hands over a pretty image200OK
        "Do you want a picture of it in the green shorts? Or with the new stylish jeans? Or in the fluffy dress? Or ..."300Multiple Choices
        "Sorry, I gave the teddy to the son of my neighboor just over the street. Ask him."301Moved PermanentlyNote that you will never see the teddy bear here again.
        "Hmm, let me think. Where did I put it? Hmmm... - Oh right, it's at the neighboor next door."302FoundNo information about how long the teddy bear will stay there.
        "Walk around the house, I'll give it to you through the front door"303See OtherRFC: For redirects from POST to GET
        "Oh, right, that teddy bear. My son has carried it into the basement, go there and ask him".307Temporary RedirectThe teddy will come back here.
        "┐Hablas espa˝ol? Parlez-vous franšais? Sprechen Sie deutsch?"400Bad Request
        "Let me see your ID card first, please"401Unauthorized
        "Hand over the money first"402Payment RequiredNo, I'm not kidding. But as no one has a working concept for payments inside HTTP, the code description has changed to "reserved for future use".
        "YOU, Sir, won't get that from me."403Forbidden
        "I don't have no stinkin' bears."404Not FoundNot here, no information if it was here before or will be here later.
        "The bear photographer? No, he doesn't live here any more. Don't know where he's gone."410GoneGone, and won't come back, like 301. But no new URL known, so no redirection.
        ***KABOOOOOM***500Internal Server Error
        "Sorry, I don't have a polaroid."501Not Implemented
        "My parents are not at home. Please come back later!"504Service Unavailable

        Is it clear now?

        What answer do you want to give? 404 means "I don't have that, and I don't know where it is." No Redirection. 301/302/303/307 means "OK, I don't have it here, but look over there." with minor differences in WHY you don't have the requested resource. Redirection follows automatically. Or the really hard 410? "It is no longer here, and it won't come back."

        Why do you think that search engines or customers need a special treatment? If a resource is not available, send 404 or perhaps 410. If it is elsewhere, send 301/302/303/307.

        If you want to help humans navigating your page when they hit a 404 page, you could add a search form, a link to a search page, a link to a site map, or links to the most wanted pages to the 404 page. Just make sure that the 404 page is larger than 1024 bytes, or else MS IE users will only see a braindead default page generated by the MS IE.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-17 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (40 votes), past polls