Htaccess redirecting error pages - .htaccess

I want to rewrite server errors (404, 500, etc) to url like this:
example.com/error.php?n=404
example.com/error.php?n=500
how to make it?
UPD:
Is there a universal way to handle all errors?

Try this :
ErrorDocument 404 /error.php?n=404
ErrorDocument 500 /error.php?n=500

Related

How to redirect 403 errors with .htaccess

The first few lines of my .htaccess file are as follows.
ErrorDocument 400 https://www.rydercragie.com
ErrorDocument 401 https://www.rydercragie.com
ErrorDocument 403 https://www.rydercragie.com
ErrorDocument 404 https://www.rydercragie.com
ErrorDocument 500 https://www.rydercragie.com
They all work apart from https://RyderCragie.com/.htaccess. Any ideas why an error is still shown on that page and how to fix it? Thanks.
Requests for .htaccess are quite likely blocked by directives in the server config - before the ErrorDocument in .htaccess is processed.
In order to catch this, you would need to define the ErrorDocument earlier in the main server config, not .htaccess. The same applies to other requests that are blocked early by Apache, such as requests that contain encoded slashes in the URL-path, and most 500 errors.
Although any request for .htaccess in the wild is unquestionably malicious, so a minimal (default) server response is probably the best response.
However, implementing all your ErrorDocument directives as external redirects to the document root is generally a bad idea (you lose details of the request that triggered the response, bad experience for users, etc.)
Give your user a meaningful error response, without the redirect. Error documents should be served via an internal subrequest. For example:
ErrorDocument 403 /error-documents/e403.html
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

.htaccess Redirect to a page with some GET variables on error

I want .htaccess to redirect to index.php on error, but with submitting some special GET variables.
For example, on error 404 I want it to redirect to
/index.php?article=404.php
I don't know how to do that as I haven't found any good tutorial for it.
This is what I tried:
RewriteEngine on
ErrorDocument 404 /index.php?article=404.php
ErrorDocument 403 /index.php?article=403.html
It seemed to work for 404 error, but for 403 error it shown:
Forbidden You don't have permission to access [...] on
this server. Additionally, a 404 Not Found error was
encountered while trying to use an ErrorDocument to handle the
request.
How should I do it?

How to redirect 404 error page with 404 response code to a custom page using htaccess

I want to REDIRECT 404 error page to custom 404 page. For example, if http://example.com/abc.html causes 404 error, I want redirect the page to http://example.com/404.html with 404 http response code. It is possible?
So far there is NO OPTION to redirect 404 error page to a custom 404 page with 404 http response code. To get 404 http response code, you will need to display custom 404 error page content without redirection like following code:
ErrorDocument 404 /404.html
And if you use following code, the 404 error page will be redirected to the custom 404 page with 302 http response code
ErrorDocument 404 http://www.yourdomain.com/404.html
Since you are using Apache, you can use the ErrorDocument directive (docs).
You are encouraged to use an internal rewrite for this, so the client receives the correct statuscode for the request (404). If you make it redirect to this page, the client will receive a 301 or 302 header instead for the non-existing page, which can confuse automated webservices, such as webcrawlers.
To make it internally rewrite:
ErrorDocument 404 /404.html
To make it externally redirect
ErrorDocument 404 http://yoursite.com/404.html

Custom 404 error page is generating a 500 error

For security reasons, I am making it so that all php files generate a 404 error, then using a custom 404 error page, like so:
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
ErrorDocument 404 /error.html
Any php script I go to returns a custom 404 error page, as I would like, but below that it says:
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
But when I go to a page that actually doesn't exist (lets say http://localhost/Hello/world.html) I get the error page I want.
I'm confused, what am I doing wrong. Also, I would like to be able to use a php pagefor my custom ErrorDocument, but I'm not sure if that's possible.
You should try a redirect match:
RedirectMatch 404 ".*\.php"
This will pass the Not Found Error, and throw the appropriate ErrorDocument.

404 errors being redirected to the homepage instead of custom 404 page

I have a site hosted on an Apache server. I have created some custom error pages and the following text at the top of my .htaccess file:
ErrorDocument 404 404.html
ErrorDocument 500 500.html
ErrorDocument 401 401.html
I have also tried,
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
Both the htaccess file and the custom pages are in the root directory of the server.
The problem is that when I enter a garbage url (where I would expect to see my custom 404 page) I'm simply being redirected to my index page.
Try if your server is properly set up to parse and process .htaccess files in the first place (i.e., check if AllowOverride + AccessFileName directives are correct). For example, write some stuff in that you know will work and look if it actually gets executed (like a ridiculous rewrite rule). Also, look up your httpd log files for errors.
If it does get executed properly, the problem might be that your server is setup not to allow all kinds of overrides with .htaccess files. Your syntax however, is basically correct.

Resources