URL changes when using ErrorDocument 403 in .htaccess - .htaccess

I have written a .htaccess file to make sure that visitors are always redirected to https and to www. In addition to that I have also added a custom html page for 404 errors.
When visitors try to access a forbidden file I want them to see my custom 404 message, as to not reveal that the path contains a forbidden file.
Here is the problem. When writing for example "example-domain.com/.htaccess" (no www or https) in the browser, the URL in the address field in the browser changes to "https://www.example-domain.com/missing.html". But I want it to say "https://www.example-domain.com/.htaccess" while displaying my 404 page.
It works for 404 errors. But when typing in a path in the address field which both triggers the 403 error and fulfill at least one of the rewrite conditions in my .htaccess file (missing https and/or www) I experience the above described problem.
Here is the code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example-domain.com [NC]
RewriteRule ^(.*)$ https://www.example-domain.com/$1 [L,R=301,NC]
ErrorDocument 403 /missing.html
ErrorDocument 404 /missing.html
Best regards

Related

.htaccess re-directs not working on a new server

I have copied a site that that sits under www.domain.co.uk onto a new, testing server which has the url http://c4034105.myzen.co.uk (ultimately we are going to point www.domain.co.uk at this new version)
I am finding that redirects don't appear to work. Here is part of the .htaccess file
RewriteEngine On
ErrorDocument 404 /error.php
#CONTACT
RewriteRule ^contact$ contact.php [L]
RewriteRule ^contact/$ contact.php [L]
The first 404 redirect is working.
However, when the url http://c4034105.myzen.co.uk/memorials/contact is generated we want it to redirect to http://c4034105.myzen.co.uk/contact but it isn't. When it was under its domain.co.uk it worked fine.
What's wrong please
Thanks
the rules you posted only add the .php extension to http://www.example.com/contact, there's no rule which redirects http://example.com/memorials/contact to http://example.com/contact.
here's one:
RewriteRule ^memorials/contact/?$ /contact [L]

htaccess redirect if URL is bad - how to do this?

I want to redirect the links which are bad, getting 404 errors.
Let's say I have the following url:
http://www.example.com/foobar
If the above url is bad, I want it to redirect it with .html suffix
http://www.example.com/foobar.html
So basically I want to make sure URL is bad and it doesn't end with .html, then only apply the rule, which is same URL with .html suffix.
UPDATE: It's not just foobar.html, there are thousands of pages dynamic, so simple use of /foobar.html to redirect will not work.
Examples of what i am looking for
below url will go to standard 404 page
http://www.example.com/this-is-bad.html
below url will go to it's .html page
http://www.example.com/this-is-bad
below url will not redirect, because of slash at end
http://www.example.com/foobar/
below url is working and SHOULD NOT get redirected anywhere. This is not a file but a dynamic page.
http://www.example.com/this-is-good
Thanks
Just use ErrorDocument 404:
ErrorDocument 404 /foobar.html
If you want full redirect (change URL in broser) then useL
ErrorDocument 404 http://www.example.com/foobar.html
This sill redirect all 404 URLs to /foobar.html
UPDATE: Try this rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /$1.html [L,R=302]

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

.htaccess redirect to 403 error

I have a website which watermarks photos (you may already know from previous q&a's). The orignal photos I use are secured using .htaccess using this code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule \.(gif|jpg|png)$ /error.gif [L]
When the user attempts to access the file it comes up with ERROR 404 which is fine but is there any way to get it to redirect to ERROR 403? (error.gif exists and is a 1x1 white pixel.)
I know .htaccess uses this to access 403 errors:
ErrorDocument 403 /error-docs/403.shtml
I have created this file in the area, and have added the above line(s) together but it still redirects to ERROR 404?
I'm not very well educated with htaccess, so any help with this will be highly appreciated.
Full Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule \.(gif|jpg|png)$ /error.gif [L]
ErrorDocument 403 /error-docs/403.shtml
Thanks,
David
To return a 403, include an F in the brackets where the L is:
RewriteRule \.(gif|jpg|png)$ - [L,F]
You can replace the /error.gif with a - which stops url rewriting since you have a separate document that gets served for the 403 so the error.gif doesn't do anything. That also means you can get rid of the RewriteCond, too.

Rewrite path before processing directory?

I'm having a small problem with my htaccess files. Currently, it redirects everything back to index.php for processing, except when someone tries to access an actual directory. When the directory exists, it displays the 403 error page instead of rewriting the path to index.php like it's supposed too. What can I modify to make it always go to index.php when the files are accessed via the internet, but still load the correct files in PHP on the server?
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule !^(.*) index.php [L]
Sample file structure:
Web Directory
- com
- source
- index.php
- TEST.HTML
The folders such as 'com' and source' will display the 403 because the user doesn't have access to them. The files such as 'index.php' and 'TEST.HTML' execute normally. I want my htaccess to redirect everything in the Web Directory folder back to the index.php file, no matter what.
I think you want this instead:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This was on the assumption that you didn't want to be able to access TEST.HTML directly and didn't want to change the URL in the user's browser. If either of those assumptions were wrong, let me know and I'll update the answer with the appropriate rewrite information.

Resources