If I mistype a URL for one of my sites and intentionally pull up the 400 Bad Request, I'm getting this message:
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
which is standard and expected, except I have a 404 page setup and defined using .htaccess:
ErrorDocument 400 http://www.mywebsite.com/error
ErrorDocument 403 http://www.mywebsite.com/error
ErrorDocument 404 http://www.mywebsite.com/error
Why isn't the browser recognizing this on a 400 error?
My 404 page is working when typing in a non-existent URL.
ErrorDocument 404 http://www.mywebsite.com/error
You shouldn't define your ErrorDocument directives with an absolute URL. In doing so it will trigger an external 3xx redirect to the error document, not an internal subrequest (recommended). By triggering an external redirect you lose all the server variables associated with the document that triggered the error (status code, requested URL, etc.).
However, this may or may not be related to your current query.
Do you have exceptions in your .htaccess file to allow unconditional access to your error documents?
Assuming you have the error page in the root directory.
If you are looking this in php/html and you have condition to not to show filename extension in the url, then your .htaccess should look like below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
ErrorDocument 404 http://www.mywebsite.com/error
(or)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
ErrorDocument 404 http://www.mywebsite.com/error.php(error.html)
Otherwise if you are showing the file extension in the url, then it should be something like below
ErrorDocument 404 http://www.mywebsite.com/error.File Extension
Example php error file: ErrorDocument 404 http://www.mywebsite.com/error.php
Let me know if issue is fixed and accept my answer.
Related
Try to set 404 page not found error page by htaccess.
Issue is if we are searching for
https://www.rsseosolution.com/suraj.html ... Its redirect perfect to 404.php.
But if we are searching for
https://www.rsseosolution.com/suraj.php ... Its not redirecting to 404.php and giving showing simple text message "File not found.".
In short problem is if extension is not php then its redirecting fine but if .php then its showing File not found.
ErrorDocument 404 https://www.rsseosolution.com/404.php
Options +FollowSymLinks
RewriteEngine on
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule seo-package-(.*)-n-(.*)\.php$ seo-package-detail.php?id=$1&name=$2 [NC,L]
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [NC,L]
RewriteRule tutorial/(.*)-(.*)\.php$ tutorial.php?topic=$1&id=$2 [NC,L]
RewriteRule blog-page-(.*)\.php$ blog.php?page=$1 [NC,L]
RewriteRule seo-tutorial-(.*)\.php$ seo-tutorial.php?page=$1 [NC,L]
RewriteRule frequently-ask-question-faq-(.*)\.php$ frequently-ask-question-faq.php?page=$1 [NC,L]
What i am looking for is ....... redirect all not found or wrong URL to 404.php.
What i am missing here.
I think because we are rewriting some URLs with .php extension from htaccess and thats why for PHP its saying file not found but for other its redirecting perfectly to 404.php.
Is there any ways to set 404.php for all not found pages and URL without change any extension (.php) of previous written file by htaccess.
The problem is not to do with your current directives in .htaccess.
The problem is mostly likely due to your server config and the way PHP is implemented on your server. If requests for .php files are proxied to a backend server for processing as is often the case with FastCGI type configs then this 404 Not Found message is likely coming from the backend server and not your Apache server.
Ordinarily, you would solve this with the ProxyErrorOverride directive in the reverse proxy config:
ProxyErrorOverride On
If you don't have access to the main server config then you may be able to trigger the 404 early using mod_rewrite in .htaccess, before the request is sent to the proxy server.
For example, before your existing mod_rewrite directives:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
However, this is strictly a "workaround", if you have access to the server config then using ProxyErrorOverride (as mentioned above) is preferable.
Aside:
ErrorDocument 404 https://www.rsseosolution.com/404.php
By specifying an absolute URL in the ErrorDocument directive this triggers an external (302) redirect for the error document (an additional request), which is generally undesirable (and you need to manually set the 404 response code). It is far better to issue the error document as an internal subrequest instead:
ErrorDocument 404 /404.php
I use the following code to serve extensionless URLS. By that I mean anyone requesting myfile.php will be served myfile
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I also use the following so I can use a custom 404 page:
ErrorDocument 404 /error.php
All that works fine until someone types in an extentionless URL which doesn't exist, such as "/abcdefghijkl" In this case Apache returns a blank page with "File not found" which isn't very helpful to users! In such a case, I want the error page (error.php) to be displayed. Is it possible?
My htaccess file currently handles 404 errors as following:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.html [L]
I did not use the ErrorDocument, because it redirects to an absolute error path.
I want to stay at the same url but show a custom template for a 403. Currently the 403 shows the default error page of apache... I would like to show a custom error document here too.
How would I do this?
Thanks for your help ;)
You can use ErrorDocument with a relative target path . With a relative path ErrorDocument directive won't redirect your request but instead show the error page without changing the url.
ErrorDocument 403 /403.php
I use this line in my htaccess file for returning index.html in any case of 404:
ErrorDocument 404 /index.html
that work but the file return in the browser with 404 code.
how can I change this line in my htaccess for return 200 code with the index.html file?
ErrorDocument tells your httpd which document to send alongside the 404 error code. If you want to always send the index.html file, you can use mod_rewrite
Here is an example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html
This rewrites (internally) all requests that don't map to a file or directory to your index.html page.
Instead of ErrorDocument 404 /index.html directive you can use FallbackResource:
FallbackResource /index.php
As per manual about FallbackResource:
Use this to set a handler for any URL that doesn't map to anything in your filesystem, and would otherwise return HTTP 404 (Not Found).
So I know this may seem a little strange but I for sake of consistency, I would like all my urls to appear in this form:
http://example.com/page/
So far I have gotten the regular pages working but I cannot seem to get the error pages working properly.
If the user visits a page or directory that does not exist, I would like the browser to hard redirect to:
http://example.com/404/
This directory, however, will not actually exist. The real location of the error page will be under
/pages/errors/404.php
Also, although I do not need an exact answer for all the various errors (400, 401, 403, 404, 500), I will be applying whatever method is given to redirect all of these to their "proper" URL's
eg.
http://example.com/400/
http://example.com/500/
etc.
Any ideas?
Try this in your .htaccess:
.htaccess
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
The ErrorDocument redirects all 404s to a specific URL, all 500s to another url (replace with your domain).
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.
Local Redirect:
Change .htaccess ErrorDocument to a file that exists (must exist, or you'll get an error):
ErrorDocument 404 /pages/errors/404_redirect.php
404_redirect.php
<?php
header('Location: /404/');
exit;
?>
Redirect based on error number
Looks like you'll need to specify an ErrorDocument line in .htaccess for every error you want to redirect (see: Apache ErrorDocument and Apache Custom Error). The .htaccess example above has multiple examples in it. You can use the following as the generic redirect script to replace 404_redirect.php above.
error_redirect.php
<?php
$error_url = $_SERVER["REDIRECT_STATUS"] . '/';
$error_path = $error_url . '.php';
if ( ! file_exists($error_path)) {
// this is the default error if a specific error page is not found
$error_url = '404/';
}
header('Location: ' . $error_url);
exit;
?>
Put this code in your .htaccess file
RewriteEngine On
ErrorDocument 404 /404.php
where 404.php is the file name and placed at root. You can put full path over here.
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that (or any other rules that you have):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
In your .htaccess file , if you are using apache you can try with
Rule for Error Page - 404
ErrorDocument 404 http://www.domain.com/notFound.html