.htaccess ErrorDocument Redirect - php

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.

Related

Deny access to a folder from direct url

So I've found many posts here on how to do a similar action, however, I'm still confused. So here is my question. I want to have access to www.mywebsite/folder/index.html but if I were to remove the index.html and just type in www.mywebsite/folder/ I don't want to have access to that. How can I go about doing this? I read that I can use an .htaccess file with deny from all but that restricts me from the whole folder and I can't access the index.html.
Please let me know if there is a solution or another post that I missed that outlines the exact situation.
Thank you!
Which Apache version are you using? In 2.4, you can use DirectoryIndex disabled to stop it from automatically serving up the index.html, and combine that with Options -Indexes.
In lower versions disabled does not exist yet, so using mod_rewrite with a simple RewriteRule that forbids access when the exact folder path (with or without trailing slash) was requested should do it,
RewriteRule ^folder/?$ - [F,L]
To control what error message the user gets to see in each case, specify the ErrorDocument for the 403 Forbidden status code.

.htaccess rewrite rule redirect to 404 page

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

ErrorDocument redirection from any directory to a file with relative links - most compact way

I have a php file that works like a custom message displayer. I pass some variables with GET and it displays a message based on that. It also works as a 404 error page. The problem is; it has relative references, which are highly desirable because of portability, but they don't seem to work, after my ErrorDocument redirection to that file.
file's location: /a/b/c/custom_message_displayer.php
When httpd.conf is set to:
ErrorDocument 404 /a/b/c/custom_message_displayer.php?e=404
and the error comes from a subdirectory eg. /a/blah, the active directory is in this case /a/, not the /a/b/c/ to where ErrorDocument is redirecting, and that results in mislinking in the file (relative css, js, include() paths).
I made a workaround that uses an additional file. But i'm concerned about this, and wonder if there's some more simple way of doing this. Perhaps an httpd.config-based redirection that also changes the active directory?
The workaround:
ErrorDocument 404 /additional_file.php → this file redirects using:
header('Location: /a/b/c/custom_message_displayer.php') also preserving/forwarding the GET query.
The file then gets executed in it's directory, and all relative links work.
Is there a simplier way of doing the above? Thanks for any bit of advice.

Create error pages and modify htaccess?

Iam trying to create error pages for my website, I created an .htaccess file with the following code:
ErrorDocument 403 /403.htm`
ErrorDocument 404 /404.htm
I also created the html files, I uploaded it in the root directory but it doesn't work.
Coould anyone tell me if I am doing anything wrong?
Thanks in advance
Blockquote
First, I would try something like adding this directive, and testing a 404 to see if it works:
ErrorDocument 404 "Sorry, no file here"
If you don't get that error, then in all likelihood your .htaccess file isn't being processed. You need to make sure that it's enabled on your server, etc. It is also possible that your ErrorDocument directive is specified somewhere that it isn't being read for the context where you are testing.
If you do get that error, the next thing I would do is make sure that your error documents are where you think they are. You mention that you uploaded them to the root directory of your project, so I would try browsing to them and see what happens.

I have the URL www.something.com/HereWeGo, I want to get HereWeGo without the 404 error

Consider the URL www.something.com/HereWeGo. When someone enters the url, the site shows a 404 error since the directory does not exists. I can parse the URL to get the query and HereWeGo, but cannot stop it from going to a 404 error.
How can I achieve it by PHP, if it is possible to avoid .htaccess now
Thanks
Jean
You will need to rewrite your URLs so /something directs to index.php?q=/something, the thing is your web server usually throws a 404 by default if the file / directory cannot be found and there are no RewriteRules associated with it, even if you can parse the URL from the error page and find the information you need.
Is there a valid reason why you can't use .htaccess? I would strongly suggest you use it if you can. Not least because at the moment a 404 will mean Google will not index your page.
This article will help you.
You just can't avoid "touching" .htaccess, at least for rewriting all requests to single file ( or defining default 404 file ). You are using Apache, not PHP for the server, right?
Take Kohana 3 Framework for example ( and most other frameworks ), all requests are rewritten to index.php file, which then handles the routes for the request.
RewriteRule .* index.php [L]
Code below will handle everything that responds with a 404 and redirect it.
ErrorDocument 404 /somefile.php
In the somefile.php you can then check if referer is 'local' ( $_SERVER['HTTP_REFERER'] ) and which query string it was about, responding with whatever you want.
You could use mod_rewrite for that specific folder, or you could use your own custom 404 page (read this article).
this is something that you need to configure on the webserver, in a .htaccess file. Take a look at the syntax for the redirect or rewrite. It isn't possible to do this in PHP alone as it is the webserver which processes the URL request.
IIRC, setting up custom 404 (and other web server errors) are part of your webserver environment.
You can't avoid a .htaccess redirect unless you change your apache (or other webservers) configuration to redirect your 404 errors to your own *.php file instead of the default error message.
You have to setup a php file, such as 404.php as your 404 error handler file. After doing this have code in 404.php that looks something like this:
if (stristr($_SERVER["REQUEST URI"],"HereWeGo")
{
//add handling code here
}
On your destination page simply call--
header('HTTP/1.0 200 OK');
--to override the default 404 status code.

Categories