htaccess return 404 instead of 200 - .htaccess

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).

Related

How to handle 403 errors without redirecting to another page

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

400 Bad Request not finding defined 404 error page

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.

different 404 for subdirectory when paths are handled by index.php

I'd like /blog/anything-that-doesn't-exist to redirect to /blog, while any other 404s are handled by Drupal.
My htaccess currently has:
ErrorDocument 404 /index.php
So everything is handled by Drupal. Is it possible to have a subdirectory specific 404 rule, but set that from the root directory htaccess? The /blog/ subdirectory is generated by Drupal, so I can't put an overriding htaccess in there.
You can use the following code in your Root/.htaccess file :
RewriteEngine On
#If /blog/foo is not a directory
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-d
#And /blog/foo is not a file
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-f
#Then redirect /blog/foo to /blog
RewriteRule ^blog/(.+)$ /blog/ [NC,L,R]
It is worth noting that, the Execution time of RewriteRule and ErrorDocument directive is diffrent. RewriteRule directive executes before the ErrorDocument directive, so any request for /blog/bad_url will be handled by the rule above, and all other 404 requests will be handled by the ErrorDocument.

Rewrite URL after redirecting 404 error htaccess

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

htaccess 404 redirect not working

I am trying to setup a custom 404 page for my website using: ErrorDocument 404 /404.html but when I add this, going to domain.com/dslkjflsdkjflskdjf doesn't redirect to the 404 page. Instead, it redirects to my homepage where mostly everything on the page is broken.
I'm wondering if this mod_rewrite.c module is the cause of this, I've tried modifying it (taking L out, adding the redirect inside the module, etc) but no luck.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
I'm not sure about this but I think your server won't recognize a 404 error because every request is redirected to your index.php. You have to determine in the index.php if there is a 404 error and send the header with the header() function.

Resources