htaccess file is bypassing my 404 error page

Discussion in 'Site & Server Administration' started by bluewaves, Jul 21, 2010.

  1. #1
    I'm trying to get my 404 error page to work, but my htaccess file is preventing it from working.

    # For security reasons, Option followsymlinks cannot be overridden.
    #Options +FollowSymLinks
    Options +SymLinksIfOwnerMatch
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^coolchecks\.net
    RewriteRule ^(.*)$ http://www.coolchecks.net/$1 [R=permanent,L]
    
    
    DirectoryIndex index.php
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
     
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    Redirect /index.html http://www.domain.com/index.php
    Redirect /specialedition.htm http://www.domain.com/specialedition.php
    Code (markup):
    Is there something in this part of the code that is causing that?

    The purpose of the two rules is to get the domain to redirect to index.php and show domain.com in the url.

    Thanks!
     
    bluewaves, Jul 21, 2010 IP
  2. mcfox

    mcfox Wind Maker

    Messages:
    7,526
    Likes Received:
    716
    Best Answers:
    0
    Trophy Points:
    360
    #2
    You have to tell it where to find the 404 page:

    ErrorDocument 404 /404page.html
     
    mcfox, Jul 21, 2010 IP
  3. bluewaves

    bluewaves Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm sorry to be so dense, but how do you tell it to do that?
     
    bluewaves, Jul 21, 2010 IP
  4. mcfox

    mcfox Wind Maker

    Messages:
    7,526
    Likes Received:
    716
    Best Answers:
    0
    Trophy Points:
    360
    #4
    I've already told you lol.

    ErrorDocument 404 /404page.html
    Code (markup):
    The same with other error pages such as 401, 501, etc if you want.

    The ErrorDocument 404 part tells your server that this is what you want to serve as an error page for 404 errors and the 404page.html part tells it what. Although the ErrorDocument 404 part is required, you can call the page anything you like; e.g. spacebunny.html, spockwashere.html, anything.

    ErrorDocument 404 /spacebunny.html
    Code (markup):
    If you decide to put your error pages in a subfolder you would do this:

    ErrorDocument 404 /subfolder/spacebunny.html
    Code (markup):
    Here's further reading to get you started:
    http://www.javascriptkit.com/howto/htaccess.shtml

    So here's your htaccess with added lines for the 401 and 404 error documents. It's also good practice to make sure your site doesn't inadvertently allow someone to view everything in a folder so it's always worth adding: Options -indexes

    
    #Your 401 error page
    ErrorDocument 401 /your401page.html
    
    #Your 404 error page
    ErrorDocument 404 /your404page.html
    
    #turn off folder listings
    Options -Indexes
    
    # For security reasons, Option followsymlinks cannot be overridden.
    #Options +FollowSymLinks
    Options +SymLinksIfOwnerMatch
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^coolchecks\.net
    RewriteRule ^(.*)$ http://www.coolchecks.net/$1 [R=permanent,L]
    
    
    DirectoryIndex index.php
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
     
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    Redirect /index.html http://www.domain.com/index.php
    Redirect /specialedition.htm http://www.domain.com/specialedition.php
    Code (markup):
     
    Last edited: Jul 21, 2010
    mcfox, Jul 21, 2010 IP
  5. bluewaves

    bluewaves Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for that explanation. I follow your logic. I tried it though and it still didn't work.
     
    bluewaves, Jul 21, 2010 IP
  6. mcfox

    mcfox Wind Maker

    Messages:
    7,526
    Likes Received:
    716
    Best Answers:
    0
    Trophy Points:
    360
    #6
    Do the error pages exist? This bit of code redirects any files or directories that don't exist to the index page.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    Code (markup):
    So if they don't exist or they exist in a subfolder and you haven't told it where to find them then your site will go to index.php.

    Read this: http://forum.mamboserver.com/archive/index.php?t-42366.html

    and this:
    http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html
     
    mcfox, Jul 22, 2010 IP
  7. bluewaves

    bluewaves Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you for explaining this to me.

    I used this and now it's working fine.

    Does the code look fine to you?



    ErrorDocument 404 /404.php
    # For security reasons, Option followsymlinks cannot be overridden.
    #Options +FollowSymLinks
    Options +SymLinksIfOwnerMatch
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.net
    RewriteRule ^(.*)$ http://www.domain.net/$1 [R=permanent,L]
    
    
    DirectoryIndex index.php
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.domain.net/ [R=301,L]
    Code (markup):
     
    bluewaves, Jul 22, 2010 IP
  8. mcfox

    mcfox Wind Maker

    Messages:
    7,526
    Likes Received:
    716
    Best Answers:
    0
    Trophy Points:
    360
    #8
    Yes, it seems okay except you didn't add the line I told you about to prevent folders listing their contents so I can do this:

    Show-indexes.jpg
     
    mcfox, Jul 22, 2010 IP
  9. bluewaves

    bluewaves Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I've added that now.

    So let me see if I understand what that does. Any folder that
    really has no index page will not show the files in it.

    How do you prevent people from seeing your robots file? I've
    always thought that was something no one should see.

    Thanks so much for walking me through this.
     
    bluewaves, Jul 22, 2010 IP
  10. mcfox

    mcfox Wind Maker

    Messages:
    7,526
    Likes Received:
    716
    Best Answers:
    0
    Trophy Points:
    360
    #10
    Correct. :)

    Unfortunately you can't but I wouldn't worry about it. The information in it is of little use to anyone else.

    You're welcome. :)

    Try to read up a bit more on htaccess if you can. You'll find it very useful over time.
     
    mcfox, Jul 22, 2010 IP