Create error pages and modify htaccess? - php

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.

Related

How to show custom 404 error page for the subfolder in php?

I am developing a project in which I want to show my custom error page for 404 error. The codes that I have used are working but not totally. So at first, I am showing the structure for my project then the problems I have.
My site name is www.example.com in which I have 4 files and a folder, files are index.php, about.php, error.php, .htaccess and folder name is admin.
In the admin folder I have lots of pages that I am loading with one single page in index.php. the URLs to access any page I am doing it with www.example.com/admin/index.php?page=login like this, so when page is login it shows me the login page.
Up to this everything is good, now I have used a code in .htaccess
ErrorDocument 404 /error.php
So after using it when I am trying to access a wrong URL such as www.example.com/abt.php it shows me the code written in error.php which is quite natural for my code.
So when I am trying to access again two wrong URLs like www.example.com/adminindex.php?page=login and www.example.com/admin/inx.php?page=login it is not showing me the code written in error.php rather it shows me just a message File Not Found.
I want that these two types of wrong URLs it should show me the code written in error.php.
Interestingly what I want is easily happens in localhost but not on in server.
I cannot understand why it is working perfectly fine in localhost but not on the server.
So how can I do that please help me to complete it?
I don't know that whether it can work for you or not? But you can give it a try i think it can resolve your issue which is quite similar to the previously asked question on stack overflow htaccess 404 page redirect not working for sub directory path
brilliantly answered by #anubhava. I hope it can help you to solve the issue. Also if the above does not help you out then you can also visit this 2 links SOLVED "File not found." instead of custom 404 file with php-fpm enabled & SOLVED "File not found" appears instead of 404.shtml (easy htaccess solution)
In fact there is no good method for that without using mod_rewrite.
The only solution would be to put another .htaccess to your /admin subdirectory (with a valid path for your error.php)
in httpd.conf file (the is in AppServ\Apache24\conf directory)
put this
ErrorDocument 404 /error.php
if not be ok try save the file as 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.

403 Forbidden Error display instead of a customised Error 404 page

i'm having this issue with my server, i'm trying to do a Error 404 display, but whn i go to an invalid URL i get this message.. Please where could the problem coming from?
The requested URL /church/ffff.php was not found on this server.
Additionallt, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request
thank you.
oh it's because the htaccess doesnt automaticly enabled on local host you need to do this:
click on wamp icon
apache->apache modules-> and enable rewrite_module
Whats your .htaccess look like?
You can add a custom handler like this:
In .htaccess (the top of it is best)
ErrorDocument 404 404.php
it seems like the server dont have accses to the 404 file, to fix this you should put the file in the same folder as your htaccess and instead of writing a full path(/c:wamp/www/church/404.php) you should just write the file's name - 404.php

How to create 404 error page in php

I am using php. I have been to create a custom error page using .htaccess file , the problem is i was not able to upload it on the server using ftp, even after trying in so many ways, so Is there any solution for this, so that I can create a custom error page without the ".htaccess" file.
What do you mean, "create a custom error page"? If you want to provide one to replace the default Apache 404 page, you will need to tell Apache to use YOUR file instead of serving up the default response. The standard method for that is to use a .htaccess, and have
ErrorDocument 404 /uri/leading/to/your/script.php
relevant docs here.
Simply uploading a random file will not magically make Apache use it when a 404 occurs - that's where the ErrorDocument directive comes in. As well, .htaccess does not "create" an error page. It simply tells Apache where to look when one does occur.

.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