.htaccess rewrite rule redirect to 404 page - php

I have a few folders such as php_includes templates and a few others and I am adding a .htaccess file for them all. So far what I have works if the user trys to directly access the file in the address bar, but I'd rather it send it to a 404 page.
IndexIgnore *
# no one gets in here!
deny from all
#error 404
ErrorDocument 404 /error/404.html
Is there something wrong with my .htaccess All I feel like is happening is the deny from all is taking over so the 404 won't work directly. Though I've tried this with a different .htaccess file for the main site, and the 404 isn't working in there either.
Same ErrorDocument as the others but here is how my directory is set up
-.htaccess
--public_html
--error
--404.html
--admin
So maybe I am either getting the ErrorDocument incorrect for my .htaccess that or I have the path directory incorrect. Can someone please explain what the issue here is?
note I am using 000webhost if that matters sometimes I see them doing some silly stuff, and this is just a testing server.

Since you are using deny directive it sends error code 403.
Declare your custom handler for 403:
ErrorDocument 403 /error/404.html

The deny from all directive will generate a 403 error.
So just do this
ErrorDocument 403 /error/404.html

Related

ErrorDocument 404 /404.php is not working in .htaccess file in PHP

I have a .htaccess file in the root directory and also 404.php file there. Content of my .htaccess file is:
ErrorDocument 404 /404.php
But when I am mis-spelling my url, 404.php is not opening. Instead I am getting following message:
Not Found
The requested URL /mywebsite/ites.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
But when I tried ErrorDocument 404 google.com, it worked.
I'll consolidate my comments to this answer:
When setting ...
ErrorDocument 404 /404.php
the /404.php path may not be the absolute path to your htdocs folder root but instead the root of your filesystem. This may be, based on your configuration, e.g. /home/htdocs/ or ~ and so on.
So what one need to do is find out the absolute path and set it accordingly.
Where is your 404.php actually located in relation to your .htaccess file? Can you simply run it as a direct URL? Is the file readable by the server? Or is it in a nested subdirectory? You can also try the full URL as well:
ErrorDocument 404 http://mygreat.server/404.php
Full details in the official Apache documentation here.
You use ErrorDocument like so:
ErrorDocument <3-digit-code> <action>
The <3-digit-code> is a HTTP response status code (eg. "404").
And you have three types of <action> that are triggered by what you type:
If the action begins with a "/": A local path to redirect to.
If the action is a valid URL: An external URL to redirect to.
If neither of the above: Text to be displayed. (The text must be wrapped in quotes (" ") if it consists of more than one word.)
For example:
Local path:
ErrorDocument 404 /local/path/to/404.php
External URL:
ErrorDocument 404 http://external_url.example.com/server_error.html
Custom text:
ErrorDocument 404 "Oops! We can't find that pesky file. Sorry."
You've chosen a local path, but are probably not pointing correctly to the file from the server's perspective. Note that local server paths are not what you see in your URL, and often include things like ~/htdocs/www/domainname/.
The problem is most likely that your path to 404.php is wrong, and cannot be found by your server.

Usage of .htaccess for 404 error

I am using .htaccess in my project, i just use the following code
ErrorDocument 404 /404.php
in the .htaccess inside the root folder, but it does not redirect to 404.php while trying to open the misspelt webpage.
I want to know whether there is syntax error in the .htaccess file.
I used .html file instead for .php file. but it throws the same error.
Not Found
The requested URL /ht/re was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
first you have to locate your errorpage
i.e. on local server my 404.php in root/error then
ErrorDocument 404 /error/404.php
here first slash is root of server
your server could not find 404.php thats problem so locate your 404.php

PHP - WAMP Server - Test my HTACCESS ErrorPage (not working)

IN MY LOCAL (WAMP) SERVER:
I have this .htaccess file
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 /views/oops.php
ErrorDocument 500 /views/oops.php
ErrorDocument 400 /views/oops.php
In my httpd.conf this line is Uncommented
LoadModule rewrite_module modules/mod_rewrite.so
The HTACCESS File is in the application root folder. When I visit this URL:
localhost/mywebapp/views/home.php
It works perfect, shows the home page. And if I enter something like
localhost/mywebapp/views/thisdoesnotexist OR localhost/mywebapp/thisdoesnotexist
I want it to show me my error page, but it's showing the default and ugly error page.
Thank you beforehand.
EDIT: This is what it shows on error
Not Found
The requested URL /myapp/thisdoesnotexist was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I'm guessing that the oops.php file is actually in the mywebapp folder, as opposed to the document root. The path that the ErrorDocument directive takes is an absolute URL, not relative to where the htaccess file is in. So you need to make sure the path points to the actual location of the error document:
ErrorDocument 404 /mywebapp/views/oops.php
ErrorDocument 500 /mywebapp/views/oops.php
ErrorDocument 400 /mywebapp/views/oops.php

.htaccess deny all, including the htaccess file and display 404 - how do I do it?

How do I do this in my .htaccess file for a specific directory in my document root?:
deny all access to anything (including the .htaccess too)
return a 404, not a 403 error
no files or subdirectories should be accessible or detected by humans or bots
only php access by the local host would be allowed
Seems like it would be simple. This works but throws a 403 not a 404:
deny from all
Why not create a custom error page?
deny from all
ErrorDocument 403 /error/404.html
The server will always want to throw a 403 error if someone is not authorized to view the page and that's what you're trying to do. However, you can change the ErrorDocument for error 403 to show an HTML page that says it's a 404 error.
My example has a folder in the root directory named error and an html file in it named 404.html.
Have you tried something like this? This will force a 403 Forbidden error when someone tries to view anything in DIRECTORY. PHP scripts can still access everything inside the directory. Obviously replace DIRECTORY with your preferred directory.
RewriteRule ^DIRECTORY - [F]
I do not like sending some html page that says it's a 404 error when really it is just an html page that may as well be located at Iamafake404error.html
You might try this instead:
Order Deny,Allow
Deny from all
Allow from 1.2.3.4
ErrorDocument 403 /throwaREAL404error.html
ErrorDocument 404 /throwaREAL404error.html
ErrorDocument 500 /throwaREAL404error.html
### never deliver .git folders, .gitIgnore
RewriteRule ^(.*/)?\throwaREAL404error.html+ - [R=404,L]
# 2nd line of defense (if no mod_rewrite)
RedirectMatch 404 ^(.*/)?\throwaREAL404error+
https://serverfault.com/a/510951/194845

.htaccess ErrorDocument Redirect

I am very new to Apache and .htaccess, and I had a simple question regarding how to handle 403 and 404 errors with Apache's ErrorDocument command.
Let's look at this code from an early CMS I wrote:
ErrorDocument 404 /admin/includes/access_deny.php?error=404
This will silently redirect the user to http://www.mysite.com/admin/includes/access_deny.php?error=404 when they get a 404 error.
Here is my problem, this error document will always be in the same place, with regards to the .htaccess file. However, if I need to move my installation, say to the directory subfolder, I will need to open the .htaccess file an update the to following, for it to work correctly:
ErrorDocument 404 /subfolder/admin/includes/access_deny.php?error=404
Is there a way I can have .htaccess redirect to this page admin/includes/access_deny.php?error=404 in regards to the file itself, not an absolute URL? Also, no matter where I would move the installation in the future, Apache will catch all 404 errors that are on the same level or sub-directories to this file.
Please let me know if that is not clear.
Thank you for your time.
From the manual:
URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot), or be a full URL which the client can resolve. Alternatively, a message can be provided to be displayed by the browser.
This thread may help you fix it though.

Categories