Redirecting to a 404 page - php

I have a web app, and I want to redirect users to the 404 page without them actually ever going to a 404 page. Is this possible?
I added this line of code to my .htaccess file:
ErrorDocument 404 /404.php
So when a user types in (a page that does not exist on my website):
shareit.me/jsdhjfkhoe
They are redirected to:
shareit.me/404.php
Is there a way to redirect to the 404 while the URL remains:
shareit.me/jsdhjfkhoe

Use this to pass all paths to 404.php if they do not exist and preserve the URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
Set this header in the 404.php file.
header("HTTP/1.0 404 Not Found");

This line:
ErrorDocument 404 /404.php
doesn't change the URL in the client browser since it does only internal rewrite to /404.php
I suspect you have some other rule in your .htaccess doing this full redirect. If you post your full .htaccess then I can investigate.

Actually this should do the trick:
ErrorDocument 404 /404.html
-> Try using an html element named 404.html in your root!
Have a look at this if you want to see a full implementation: https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess
Otherwise you could have a look at the documentation if anything is unclear:
http://httpd.apache.org/docs/current/mod/core.html#errordocument

If you want to make your own customized Error 404 page. Here's what you need to write down on your .htaccess.
----------- .htaccess ----------------
# 1 ---- Establish a custom 404 File not Found page ----
ErrorDocument 404 /filenotfound.php
# 2 ---- Prevent directory file listing in all of your folders ----
IndexIgnore *
----------- .htaccess ----------------
Where filenotfound.php is your own 404 customized page. Hope it helped.

Related

How can i redirect unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei) using .htaccess?

There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)
How can I redirect its to main root?
My .htaccess not working, I'm using below code in .htaccess:-
RewriteEngine On
ErrorDocument 404 /example.com
There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)
How can I redirect its to main root?
I assume /digital-marketing.php is a file on your filesystem. In which case /ercbierubcrei is additional pathname information (or path-info). By default, the PHP handler allows path-info, so /digital-marketing.php is still served but at the different URL (if that's what you mean by "unstructured link"?).
You can simply disable path-info on these URLs by setting the following at the top of your .htaccess file:
AcceptPathInfo Off
Now, any URL that contains path-info will trigger a 404 instead and your custom 404 error document will be called. (You can still override this behaviour with mod_rewrite if you wish.)
Alternatively, if you wanted to redirect /digital-marketing.php/ercbierubcrei to /digital-marketing.php (is that what you mean by "redirect its to main root"?*1) then you can do something like the following near the top of your .htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule ^(.+?\.php)/ /$1 [R=302,L]
This redirects a URL of the form /<something>.php/<anything> to /<something.php, effectively discarding the path-info portion of the URL.
(*1 - If by "main root" you do literally mean the document root of the site then that is not recommended. Google would likely treat this as a soft-404 anyway. A custom 404 with a meaningful message would be preferable.)
ErrorDocument 404 /example.com
This does not look "valid". The 2nd argument to the ErrorDocument directive should ideally be a local URL-path to the document that handles the error response.
For example:
ErrorDocument 404 /error-documents/my-404-error-document.php
A simple 301 redirect will do the job.
redirect 301 /old_url http://www.domainroot.com/
use Complete url of your site. Like this
ErrorDocument 404 https://www.yourdomain.com/example.php
Please refer to below .htaccess examples:
Redirect http://yourdomain/digital-marketing.php/apple to http://yourdomain/:
RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^ / [L,R=301]
Redirect http://yourdomain/digital-marketing.php/orange to http://yourdomain/ref?code=orange:
RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^digital-marketing.php/(.*)$ /ref?code=$1 [L,R=301]

How 404 page will work

This is my error page i dont know exactly what is but i want to redirect this kind of error to 404.html page:
I have created a 404 error page name "404.html". I want to redirect to this if no file or folder exist in my server.
Where do I have to place this page (404.html)?
Is it possible to redirect this page in .htaccess (or possible with only .htaccess)?
If is there anything more about 404 page let me know, I want to grab the knowledge.
And don't vote me down if possible of copy!
I want to map one error document file for 404 500 i am trying with this code.
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
RewriteEngine On
RewriteBase /
# map them to one error document:
RewriteCond %{REQUEST_URI} ^/404/$ [OR]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404.html [L]
I tried this code and uploaded the file in main folder www.website.com/404.html
Its easier then I expected just gave full url:after reading documentation.and it worked.
ErrorDocument 404 http://www.website.com/404.html
ErrorDocument 500 http://www.website.com/404.html
No answer needed.
Where you want on your server
Yes : ErrorDocument 404 /yourPath/404.html
Google and Apache docs are your friends.
Edit : You add an image on your question. This is a 500 Internal Server Error, and not a 404 Page not Found error. Handle it with
ErrorDocument 500 /yourPath/500.html
But if your .htaccess file is corrupted and cause this error, you first have to fix your .htaccess file.
You can specify the document for error handling inside .htaccess similar to one below
ErrorDocument 404 /404.htm
This will override apache's default 404 page. The 404.htm will be inside your root folder.
https://www.addedbytes.com/articles/for-beginners/error-documents-for-beginners/

Redirecting to custom 404 page in home directory

I am having trouble redirecting the 404 (incorrect url requests) to a 404 page (404.php) I developed and uploaded to the server.
The server has a Wordpress installation in the sub-folder /blog and all the 404 are getting redirected to wordpress blog's 404 page. I have set the redirection in .htaccess but is not working.
ErrorDocument 404 /404.php
This is what I added in .htaccess, but it still redirects to the blog's 404 instead of the 404 page in root.
How can I override this?
In my understanding ErrorDocument 404 /404.php will only work if you have not specified rewrite rule in .htaccess eg:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Because wordpress has it's own life cycle which uses URL rewrite in .htaccess to determine which page request are coming. WordPress has it's core mechanism to understand the current request. For example you have created a page name hello-word, when you go to http://your_wordpress.com/hello-word wordpress determines hello-world from URL and find in it's database whether it has hello-word page or not. If current page does not exist in database then it's mechanism automatically call theme 404 template which is located in currently active theme.
Solution
Create a file in wp-content/themes/YOUR-THEME/404.php and change it's content. You don't need to specify ErrorDocument 404 /404.php in .htaccess. Read more how to create 404 template here
I achieved it by pasting this code on top of my wordpress theme's header.php file
if ( is_404() ) {
wp_redirect( '../404.php' );
exit;
}
And now the root/main site 404 page is getting loaded instead of wordpress blog's 404 page as intended. :)
first of all make sure that the 404.php is present in the proper location and http://www.example.com/404.php is working properly.
if both 404.php and .htaccess are present in the same root folder, then try changing following line in .htaccess file
ErrorDocument 404 /404.php
to
ErrorDocument 404 /root/404.php

.htaccess redirect 403 error files to 404 error document

I have a folder named control which is contained in the root project folder named project, so the path on localhost forms like this for the control folder: http://localhost/project/control
In the control folder I put a .htaccess file that has the content:
RewriteEngine On
deny from all
At the project root folder i.e project folder i have another .htaccess file that contains the main code, including this 404 error redirection code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ errordocs/404.php [L]
Now what I want to do is that whenever 403 error is encountered when opening the control folder it should rewrite it to the same errordocs/404.php page.
You can match a statuscode with an output page with ErrorDocument
ErrorDocument 404 errordocs/404.php
ErrorDocument 403 errordocs/404.php
I was able to solve my problem, i am trying to explain it here, so that it might be helpful to others also, it is working fine for me.
in my project/control/.htaccess file i put the code:
RewriteEngine On
RewriteRule ^$ ../errordocs/404.php [L]
So whenever the user types the url localhost/project/control/ then he will see the custom 404.php file from errordocs folder which is one directory back, as my aim was to prevent direct access to the control folder. I did not use deny from all instead of that used the above procedure so that i can use the custom error pages for the 403 error, that will be independent of the document root path. Now my project folder can be named to any name the user wishes.
Note: This procedure will return 200 instead of actual 403 error. To return 403 and 404 use the below respective code:
403
RewriteEngine On
RewriteRule ^$ ../errordocs/404.php [L]
RewriteRule ^$ - [R=403,NC,L]
404
RewriteEngine On
RewriteRule ^$ ../errordocs/404.php [L]
RewriteRule ^$ - [R=404,NC,L]
redirect 403 to 404 in htaccess
ErrorDocument 403 /yourfoldererror/404.php
And in your 404.php add php code like this :
<?php http_response_code(404);?><!DOCTYPE html><html ....
With that code, the response will not 403, but 404

.htaccess not redirecting to 404.php instead shows page name

Am trying to make a custom 404 page for my website and am having .htaccess file in the root directory where am using this rule
ErrorDocument 404 404.php //I want to redirect to 404
So when I change a valid file name like home.php to home1.php it doesn't redirect me instead it echo's 404.php on that page
Side Note: 404.php is in the root directory only
This should do it
RewriteEngine on
ErrorDocument 404 http://www.yoursite.com/404.php
In your .htaccess file, you should be able to use:
RewriteEngine on
ErrorDocument 404 /404.php
You can set additional error documents using this method, but I'd put them in a separate errors directory:
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
you could do the following to 404 old pages with your htaccess
RewriteEngine on
ErrorDocument 404 /404.php
but i would personally recommend
RewriteEngine on
RewriteRule home.php /home1.php [R=301,L]
as this would do a 301 redirect from the old page name to the new page name, so any cached search engine results would still end up at the correct page instead of hitting a 404
In my case, using an Ubuntu distribution, the directive ErrorDocument has no effect if it is in the .htaccess in htdocs directory or elsewhere: it turned out that it should be put in the proper /etc/apache/sites-enabled/*.conf file, inside the <VirtualServer> directive (for example, if the website is providing https pages, inside the directive <VirtualHost *:443>).

Categories