I am getting a 403 error when my URL string it too long. See the example below.
I want to redirect it to an error page instead of displaying the default server message.
I tried the below code in .htaccess but that does not work:
ErrorDocument 403 /403.php
Sample url:
https://example.com/111111111111111111111111111111111111111111111111111111111111%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%11111111111111111111111%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks%20whschecks
Related
I am trying to redirect the user to a page when 404 error occurred, so I am writing
ErrorDocument 404 /error.html
In .htaccess file and having an error.html file in my root directory but it's giving me
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Try resetting the error document to the system default prior to setting your custom document. For example:
ErrorDocument 404 default
ErrorDocument 404 /error.html
On some shared servers, the error document is (incorrectly) preconfigured in the main server config.
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?
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
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.
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.