How do I do a immediate 404 redirect

Discussion in 'Apache' started by PHP dev, Nov 26, 2008.

  1. #1
    Hi,

    I have removed the link 'CHAPTER<num>' from my web page.
    Now I wanted to create a immediate 404 page not found rule for all the requests for this link

    e.g
    chapter1 should give 404
    chapter2 sould give 404
    and so on..

    any idea?
     
    PHP dev, Nov 26, 2008 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Adding this line to your .htaccess file:

    will direct all 404's to whatever page you specify.
     
    Colbyt, Nov 27, 2008 IP
  3. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP dev, Nov 27, 2008 IP
  4. crispycheesy

    crispycheesy Guest

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    crispycheesy, Nov 27, 2008 IP
  5. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you are right. Normally we will use 301 for this and for me 301 is working fine.
    But specifically I need a 404 in this situation, hence I opened this thread.
     
    PHP dev, Nov 28, 2008 IP
  6. Bagi Zoltán

    Bagi Zoltán Well-Known Member

    Messages:
    364
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #6
    I assume you will have possibilty to execute php files on the given webserver, so I suggest to create a check404.php file with the following content.

    <?php 
    if (preg_match("/chapter/i", $_SERVER['REQUEST_URI'])
    {
    header("HTTP/1.0 404 Not Found");}
    ?>
    PHP:
    This will execute a search in the requested URI and when a match occures in accordance with the patter it will give 404 response.

    In order to make this script execute before every request insert this code to your .htaccess file

    php_value auto_prepend_file "/www/htdocs/yoursite/check404.php"
    Code (markup):
     
    Bagi Zoltán, Nov 28, 2008 IP