Need a little .htaccess assistance


(Varied Team Members) #1

Hello RW Team & Developers… I would value a little assistance with a .htaccess setup need. Firstly I have been using the below code in my .htaccess file to correct an issue where a user manually types in the wrong url to a page ending in .php and this code displays my error.html page in their browser. Without this the error.html page does not display.

  • My site has mixed page extensions, e.g. html and php. without the below .htaccess code a user who enters the wrong url for a page ending in .php only sees a white screen with the words “File not found” not the actual error page. It has been some time since this was set up. Could someone explain, in layman terms, what this code is doing?

  • Also I am wanting to make separate error pages such as 400, 401, 402, 403, 404 and 500. Would I need to provide the same .htaccess code for each changing the first and last line to match each error page?

Kind Regards,
Dave

    RewriteRule ^error\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /error.html [L]

(Varied Team Members) #2

Hello Again… Would value any input and please feel free to ask if I have not provided enough clarity?

Also here is the the location of my 404 page, see the reference to it from my .htaccess file below.

ErrorDocument 404 /Notices/error.html

Thank You,
Dave


(Doug Bennett) #3

Not sure what you are asking, here is a site you can use to test .htaccess
http://htaccess.madewithlove.be


(Varied Team Members) #4

Hi Doug… Thank you for taking a crack at this. While your link is handy I don’t believe it will help here. The .htaccess code was something I came up with several months ago to correct the issue mentioned above. Just trying to regain an understanding as to what each of the lines are for and what this is doing to correct the issue.

Again, thanks for your input, appreciate any help I can get here.

Dave


(Varied Team Members) #5

OK… I have done further digging and will take a stab at my above code. Would value someone with more expertise to weigh in here.


RewriteRule ^error\.html$ - [L]
If the users request matches this rule they are passed right along to the error.html file.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Not exactly sure what these do but it appears they work with the above rule to determine if it is true or not and passing the request on to the next rule.

RewriteRule . /error.html [L]
I believe what this final rule does is convert the request to the proper name and file type so the the error page can be displayed in the browser.

Not 100% sure about all this but I believe I am close based on a few of my notes and my server management providers documentation. The following is a quote from one of their articles.

The ErrorDocument directive does not catch 404 errors for requests that end in .php themselves. For example, a request for /this-does-not-exist.php will not be handled by the ErrorDocument directive because the request ends in .php.

To handle 404 errors for requests that end in .php, you should use rewrite rules that direct those requests to a specific PHP script, which will then send a 404 response.

Again would value input from someone more knowledgeable then myself and I hope this is helpful to others as well.

Kind Regards,
Dave