Mask 404 page URL with .htaccess - .htaccess

I've setup a 404 ErrorDocument handler in my htaccess file, and it works fine. However, the address bar reflects the filename of that file. I'd like to use mod_rewrite to mask the actual filename and replace it with something else while still actually displaying the file.
Here's my current htaccess file:
RewriteEngine On
Options +FollowSymlinks
Options All -Indexes
ErrorDocument 404 /404.php
RewriteRule ^404.php$ /error/404 [L]

Figured it out on my own... Rather than using the 404.php file in the ErrorDocument handler, I used the fake url, then added a rewrite rule to rewrite the fake url to the 404.php file (but without redirect).
RewriteEngine On
Options +FollowSymlinks
Options All -Indexes
ErrorDocument 404 /error/404
RewriteRule ^error/404$ /404.php [L]

Related

htaccess file make redirection to another file

I have htaccess file below
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /index.html /login.html
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
Options -Indexes
Redirect /index.html /login.html
When user enters index page it should redirect to login.html page.
But it doesn't work.
Redirect /index.html /login.html
im requesting /index
If you are requesting /index then I'm not sure why you are checking for /index.html in your rule. However, you should be using mod_rewrite to construct this redirect since you are already using mod_rewrite for your internal rewrites.
So, have it like this instead:
Options -Indexes
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
RewriteEngine On
RewriteBase /
# Redirect "/index" to "/login"
RewriteRule ^index$ /login [R=302,L]
# Append ".html" if target file exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*) $1.html [L]
I've also "fixed" your .html rewrite since this could result in a 500 error for certain requests and there's no need to check that the request does not map to a directory before checking that the request does map to a file.
See the following question on ServerFault that expands on this potential issue regarding .html removal. https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

How to apply RewriteRule properly on forbidden files?

The goal
If the request is http://⋯/.htaccess,
do not display the content of that file,
show an ErrorDocument 403 instead,
rewriting only http:// to https://.
The problem
The URL is rewritten to https://⋯/403.shtml,
instead of the desired https://⋯/.htaccess.
The details
The ErrorDocument 403 and the protection of the
.htaccess are set up by the web hosting provider.
The HTTPS rewrite is set up in the .htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}/$0 [L,QSA,R=301]
<If "%{HTTPS} != 'on'">
Require all granted
RewriteEngine on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,QSA,R=301]
</If>
Set the following in your .htaccess file to reset the default 403 Apache ErrorDocument:
ErrorDocument 403 default
The ErrorDocument 403 and the protection of the .htaccess are set up by the web hosting provider.
It looks like the host has configured the ErrorDocument directive with an absolute URL to the 403.shtml document, which will naturally trigger a 302 redirect instead of an internal subrequest.

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.

SImple way to redirect 403 and 404 errors to the main page htaccess

As the title says is there any easy way to redirect every forbidden error and page not found to my main page index.php?
Here is my htaccess don't think it will be of any use but here it is:
ErrorDocument 404 /leaguenotes/administration/cms/new_patch
ErrorDocument 403 /leaguenotes/administration/cms/new_patch
Options ALL -Indexes
RewriteEngine On
RewriteRule ^([0-9/.]+)$ index.php?Patch_No=$1 [NC,L]
RewriteRule ^([0-9/.]+)&([0-9a-zA-Z_-]+)$ index.php?Patch_No=$1&tab=$2 [NC,L]
RewriteRule ^patches php/patches.php [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)$ index.php?Champion=$1 [NC,L]
You can add these 2 lines on top of your .htaccess:
ErrorDocument 404 /
ErrorDocument 403 /
This will redirect every 403 and 404 errors to your home page.
You can try this on top of your .htaccess file
ErrorDocument 403 "<meta http-equiv='refresh' content='0; url=your_redirect_page.html'/>"

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

Resources