Is It Possible to Give a 301 Instead of 404 Error?

Discussion in 'PHP' started by Nathan Malone, Dec 5, 2005.

  1. #1
    Note: This might be a post that should go in the Apache forum - I wasn't sure so I just posted it here. Feel free to move it.

    I am trying to get all error pages on my site automatically redirected via a 301 permanent redirect to the home page. In other words, I want digitalpoint.com/3feafioajjdpsfja/ to redirect to http://www.digitalpoint.com/ .

    Right now, I have a custom 404 error page set up in the .htaccess, but the problem is that the server gives a 302 redirect from the error page to the custom error page, and from there, I have a php script set up to give a 301 redirect to the home page.

    Is there any way to elminate the 302 redirect, or change it to a 301? My goal is to get a redirect that the search engines will follow from any error page to the home page.

    Thank you!
     
    Nathan Malone, Dec 5, 2005 IP
  2. Michau

    Michau Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You could try mod_rewrite for that. It allows for making such smart things. It's not easy to use, however.
     
    Michau, Dec 6, 2005 IP
  3. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Mod_Rewrite wouldn't work in this case, because I wouldn't know what url's people would type in. I simply want all url's that would normally get a 404 to instead get a 301.

    Also, could a mod please move this topic to the Apache forum? I probably should have posted it there to begin with.

    Thanks,
     
    Nathan Malone, Dec 6, 2005 IP
  4. boccio

    boccio Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    boccio, Dec 6, 2005 IP
  5. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Well, that would take care of that particular example, but then, what if someone happened to type in digitalpoint.com/aewfeioa/?

    I am looking for a way to send all pages that would normally get a 404 error (all pages that don't exist) to another page via a 301 redirect.

    I can set up a custom error page, that then redirects via a 301 to the home page, but the server gives a 302 redirect from the bad url to the custom error page, and I want to eliminate that 302.

    Thanks for the code, though, I'll use it in another project.
     
    Nathan Malone, Dec 6, 2005 IP
  6. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I use something like:

    RewriteRule ^(.*)$ process.php

    This directs all requests to process.php where I can determine the validity of the URL and get the files requested.

    -phil
     
    philej, Dec 6, 2005 IP
  7. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Yes, I think that that approach might work in my situation. Thanks!
     
    Nathan Malone, Dec 7, 2005 IP