In this article we'll discuss setting up a custom 404 error page via a .htaccess file. This will allow you to set the message that visitors to your site receive when they request a page that doesn't exist on the server.
 
If you read our previous article about 404 error not found pages it was discussed that handling 404 errors dynamically can be problematic if done very frequently. If you're running WordPress on your website, you might want to think about installing the W3 Total Cache plugin which has the ability to serve static 404 errors for WordPress.
 
It's typically recommended to handle 404 errors with a custom static HTML 404 error page instead of handling them dynamically. If you don't already have one you can learn about creating a custom 404 error page, and if you're looking to add advanced functionality to your 404 error page you can even learn about using Google's custom 404 page widget.
 
The easiest way to modify your .htaccess file would be using cPanel's file manager with the instructions below, although you can also upload a .htaccess file via FTP as well.
 

Modifying or creating a .htaccess via cPanel's file manager

 
  1. Login to your cPanel.
  2. Under the Files section click on File Manager.
  3. Select the Document Root for: option.
  4. Ensure Show Hidden Files is selected.
  5. Then click Go.
  6. file-manager-hidden-files
     
  7. Right click on the .htaccess file and select Edit.
  8. file-manager-htaccess-edit
     
  9. If the .htaccess file didn't exist in the previous step, click on New File at the top left, name the file .htaccess, and set the directory for creation to /public_html/ or the document root of your site.
  10. file-manager-htaccess-create
     
  11. You might have a text editor encoding dialog box pop-up, go ahead and just click Edit.
  12. file-manager-text-editor-encoding
     

    Now that you are ready to edit the .htaccess file, the simplest way to set a 404 error page is by directly setting a 404 error message in the .htaccess file itself:

    ErrorDocument 404 "<H1>Page not found</H1>"

    If you already have a static HTML page that you'd like to use for 404 errors you can also direcly point to that file with the .htaccess file as well:

    ErrorDocument 404 /404.html

  13. After you've made your edits, click on Save Changes, and you should see a green Success pop-up.
    file-manager-text-editor-savefile-manager-text-editor-save-success
  14. Now if you try to pull up a page that won't exist on the server such as /ThisPageWon'tExist you should see the custom 404 error message that we've configured. The first image below is the default 404 error page when a custom one is not set, and on the second one is our custom one that we've just created:404-default-error-in-browser404-custom-error-in-browser

You should now understand how to control how 404 errors are handled for your website, by either setting the error directly, or pointing to a custom 404 error page. If you need help on actually creating your custom 404 error page you would want to review our article on creating a custom 404 error page.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-04-10 2:48 am

i need htaccess to 404 page so i already putted this code

RewriteEngine onErrorDocument 404 /404.php

but this is not working.

i open ftp   after that i went /httpdocs   ....there are not puplic html folder

please guid me

Staff
9,968 Points
2014-04-10 4:02 am
Hello Rajkaran, and thanks for the comment.

If you have a /httpdocs directory instead of /public_html then that's fine. That should still be your website's document root where your website files go.

Are you able to access your 404 page directly in your web browser when going to it such as: http://example.com/404.php ?

If so, then at the top of your.htaccess file, you would just want to use this:

ErrorDocument 404 /404.php


You do not need to turn on the Apache RewriteEngine when using the ErrorDocument setting, so you don't need that part.

If your custom 404 page still isn't coming up, what happens when you try to access a page that doesn't exist? Do you get another specific error from the server? Are you running a CMS that might be handling 404 error pages internally?

Please let us know if that fixes your problem.

- Jacob
n/a Points
2014-05-21 9:17 am

Thanks all of for helping.... realy this article very helpfull for me.

n/a Points
2014-08-08 12:12 pm

Hi also had the same problem

Create the .htaccess file on cPanel it easy than through FTP

Staff
9,260 Points
2014-08-08 12:30 pm
Hello SILVESTER,

Thank you for your comment. Here is a link to our guide on Creating a file, it explains how to create a file in File Manager and FTP.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-08-26 1:45 am

File Manager or Legacy File Manager

Staff
19,241 Points
2014-08-26 8:53 am
Hello Ron,

I am not sure what you are asking. Were you referring to John-Paul's link on making a file in the File Manager? The steps would be the same in Legacy File Manager, but the interface would appear differently.

Kindest Regards,
Scott M
n/a Points
2014-04-10 7:51 am

hello sir

i got your reply but this is not running 404 page.....i am sending .htaccess file code...

//-----------.htaccess code---------------------------

RewriteEngine on

ErrorDocument 404 /404.php

 

//------------------end of code--------------------------

Staff
8,910 Points
2014-04-10 7:54 am
I see that you are referencing a file labeled 404.php within your .htaccess file. Have you named your file 404.php? If not, you will need to change this to the name of your file.
Staff
9,968 Points
2014-04-10 10:47 am
Hello again Rajkaran,

As I said in my previous comment, you do not need to turn on the Apache RewriteEngine. For your 404 page to function, place this on the very first line of your .htaccess file, at the very top:

ErrorDocument 404 /404.php


- Jacob

n/a Points
2014-04-24 2:38 am

Hello sir

this is my website bhopalmpproperty.com ....actualy have a problem if i put www.bhopalmpproperty.com then genrate errors "The connection was reset" and if i enter bhopalmpproperty.com on navigation bar then good...

i have htaccess there are some code:

# -FrontPage-
RewriteEngine On
RewriteCond %{HTTP_HOST} !^bhopalmpproperty\.com$ [NC]
RewriteRule ^(.*)$ http://bhopalmpproperty.com/$1 [R=301,L]
 
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
 
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthUserFile /home/bhopalmp/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/bhopalmp/public_html/_vti_pvt/service.grp
 
please help me whats problem.....why not runing if i insert www
Staff
8,910 Points
2014-04-24 8:22 am
When visiting both bhopalmpproperty.com and www.bhopalmpproperty.com, I was unable to produce any errors on your site. It looks like the issue is solely isolated to your specific connection or browser. I recommend clearing your browser cache as there may be something in there that is causing the issue.
n/a Points
2014-05-20 3:11 am

hi how can i redirect subdomain files to 404 error

for eg: 

subdomain.primarydomain.com/1.html 

 

Staff
8,910 Points
2014-05-20 8:42 am
To clarify, are you referring to redirecting the entire subdomain to a 404 error? Are you referring to just 404s for the subdomain going to a specific 404 page?
n/a Points
2014-05-21 8:12 am

it worked :) 

thanks a million  @jeffma

n/a Points
2014-05-21 9:15 am

Thanks all of for helping.... realy this article very helpfull for me.

n/a Points
2014-05-22 12:28 am

thanks !!

that worked 

n/a Points
2014-05-27 12:28 am

We are getting 404 not found error intermittenly in all the pages of site, but when i do refresh i am getting the page.

1. is it a webserver issue or a load balancer issue? the direct apache instance URL is working well.

2. is there a way we can refresh automatically ( 3 Retrys) whenever the we get 404 notfound issue ?

3. What Rewrite engine will do?

Please provide your inputs.

Staff
8,910 Points
2014-05-27 8:45 am
It sounds like you're using a non-standard setup. Could you clarify a bit more about your server configuration?
n/a Points
2014-06-09 3:39 pm

thanks... this was so helpful

n/a Points
2014-06-10 1:09 am

Hi guys

I am using insert query and also using mysql_insert_id() function but it is not giving response why i used oops..if i use simple query then its working but if i do through oops then not working....

$query=mysql_query("insert into table name (id,name,status) values('','test',1)");

mysql_insert_id();

now working but if i use this

$obj=new Model;

$obj->_id_name="id";

$obj->_table_name=TABLE;

$obj->_new_row=true;

its not working...i know you are confuss but simply unserstand if i do through oops then mysql_insert_id() not workind why?

Staff
8,910 Points
2014-06-10 8:18 am
It looks like you are calling the mysql_insert_id() function but are not actually doing anything with it. Is this your complete code?
n/a Points
2014-06-10 9:26 am

yes this is not full code of .....

Now this is full code

if (empty($app->errors)) 

{

$tbl=new Model;

$tbl->_id_name="city_id";

$tbl->_table_name=CITY_TABLE;

$tbl->_new_row=true;

 

//--------------------- check duplicate records ---------------------------------

if($_REQUEST["hdnAction"]=="Add"){

$sSql=" where city_title=? and city_cid=? and city_sid=? ";

$Category= $tbl->checkduplicatrecord($sSql, array($_REQUEST["city_title"],$_REQUEST["city_cid"],$_REQUEST['city_sid']));

}

else{

$sSql=" where city_title=? and city_cid=? and city_sid=? and city_id<>?";

$Category = $tbl->checkduplicatrecord($sSql, array($_REQUEST["city_title"],$_REQUEST["city_cid"],$_REQUEST['city_sid'],$nId));

}

//---------------------------------------------------------------------------------

if($Category==false){

$tbl->set_attribute();

 

 

$tbl->city_status=$_REQUEST['city_status'];

 

if($_REQUEST["hdnAction"]!="Add")

{

$tbl->_new_row=false;

$tbl->city_id=$_REQUEST["id"];

$sAction="Edit";

}

        echo mysql_insert_id();

$tbl->save();

//echo $tbl->_sqlQuery;

//$app->redirect("manage_city.php");

$error_type="s";

$error_msg="City Successfully Added";

}

else 

{

$error_type="e";

$error_msg="City already exists";

}

 

}

 

 

Staff
8,910 Points
2014-06-10 9:38 am
It doesn't appear that you are running the INSERT MySQL command before you call the mysql_insert_id(); function which is causing no data to be displayed.
n/a Points
2014-06-10 9:43 am

thnx for guidance 

thank JeffMa :)

n/a Points
2014-06-11 1:30 am

Hi 

i am beginer of smtp email,i used this smtp code an others on landing page there is working but if i use same code an other then genrate this error " SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. "

can you please help me.

i am using this code

sender.php

-------------------------------

<?php

require("class.phpmailer.php");

ob_start();

if(isset($_REQUEST['yourname']) && $_REQUEST['yourname']!="")

{

$to  = 'director@pimrindore.ac.in,pimr_pg@pimrindore.ac.in,director_ug@pimrindore.ac.in,info@emaxglobal.com,ravi@emaxglobal.com,raj@emaxglobal.com' . ', '; // note the comma

 

// subject

$subject = "PPC Adword Enquiry";

 

// message

$message = '

<html>

<head>

  <title>enquiry </title>

</head>

<body>

<table width="100%" border="0" cellpadding="2" cellspacing="2">

  <tr>

    <td colspan="2">Candidate Information as below:-</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">Interest :-</td>

    <td valign="top">'.$_REQUEST['interest'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">Name:-</td>

    <td valign="top">'.$_REQUEST['yourname'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">Email Address:-</td>

    <td valign="top">'.$_REQUEST['youremail'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">Contact No.:-</td>

    <td valign="top">'.$_REQUEST['yourphone'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">City  :- </td>

    <td valign="top">'.$_REQUEST['city'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:90px;">Message :-</td>

    <td valign="top">'.$_REQUEST['maintext'].'</td>

  </tr>

  <tr>

    <td valign="top" style="width:180px; float:right;">Visit Web Page          <a href="/support/pimrindore.ac.in/lp/" target="_blank">Click here</a></td>

    </tr>

</table>

 

</body>

</html>

';

$from=$_REQUEST['youremail'];

 

$mail = new PHPMailer();

//$mail->SMTPSecure    = 'ssl';

$mail->SMTPDebug=false;

$mail->Host = 'ssl://smtp.gmail.com';

$mail->Port = 465;

$mail->IsSMTP(); // send via SMTP

//IsSMTP(); // send via SMTP

$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = "support@elensoft.com";//"digital.mkt@ssglobalgroup.com"; //  "mr.mattkuchar@gmail.com";// SMTP username

$mail->Password = "newmoon";//"bspartners_"; // "Year2013!";// SMTP password

$webmaster_email= "vaibhavkorea@gmail.com"; //Reply to this email ID

 

$name='vaibhavkorea@gmail.com'; // Recipient's name 

$mail->From = $name;

$mail->FromName = "PIMR INSTITUTE";

//echo $message;//$mail->AddAddress($email,'');

//$mail->AddAddress('vaibhavkorea@gmail.com','Enquiry');

$mail->AddBCC('vaibhavkorea@gmail.com','VAIBHAV');

$mail->AddBCC('director_ug@pimrindore.ac.in','Director');

$mail->AddAddress('director@pimrindore.ac.in','Director');

$mail->AddAddress('pimr_pg@pimrindore.ac.in','PIMR');

$mail->AddAddress('raj@emaxglobal.com','RAJ');

$mail->AddAddress('ravi@emaxglobal.com','RAVI'); 

 

 

//$mail->AddReplyTo($webmaster_email,"Webmaster");

$mail->WordWrap = 50; // set word wrap

 

//echo $subject;exit; 

$mail->IsHTML(true); // send as HTML

$mail->Subject = $subject;

$mail->Body    = $message; //HTML Body

//$mail->$SMTPDebug = true;

if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; }

else               { echo "Message has been sent"; }

 

 

//-------------------------------------------------end of email code------------------------------------------------

//header("location:thank-you.html");

?>

Staff
8,910 Points
2014-06-11 7:56 am
I noticed that you had the following line defined as the host:

$mail->Host = 'ssl://smtp.gmail.com';


You do not need the ssl:// before the host name. Remove it and it should connect just fine.
n/a Points
2014-07-07 2:11 am

public_html/.htaccess (I go this file edit)

But show this code...

 

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

 

I now how to put 404.html code....

thanks

Staff
9,968 Points
2014-07-07 3:19 am
Hello Minura,

You should be able to use this at the top of your .htaccess file:

ErrorDocument 404 /404.html



That code that you have looks a bit off, typically you'd see it written as:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]



Because the RewriteCond %{REQUEST_FILENAME} !-f line is saying if the physical file doesn't exist proceed to the RewriteRule. However if you requested test.php and that script didn't physically exist, you wouldn't want to then use this:

RewriteRule ^(.*)$ $1.php



As that would tell the server, I know test.php doesn't exist, but go ahead now and try to load test.php and you would get stuck in a redirect loop.

Most CMS applications will have you redirect all missing URLs to a index.php script or similar to process the page not found error themselves. So it depends on what application you're using as to how exactly you would need things setup in your case.

- Jacob
n/a Points
2014-07-08 8:12 am

I have w3 total cache but I don't think it's working because you guys keep emailing me about usage issues with my 404 error pages.  I still have w3 cache enabled and configured according to your directions.  I also hadded the static 404 error code you mention in this post and it's not showing up.  Not sure what the solution is.

Staff
19,241 Points
2014-07-08 8:43 am
Hello sheresed,

Setting up the static 404 page as shown in the article should work for you. Please check to make sure you have done that all correctly. Also, be sure to place the code in the .htaccess file at the top so that no other code will run before it in the case of a 404.

Kindest Regards,
Scott M
n/a Points
2014-07-08 8:50 am

I did.  my site is http://efficio.biz  Maybe I missed something but I have the code at the very top.

Staff
19,241 Points
2014-07-08 10:16 am
Hello sheresed,

I did take a look at your site and it seems there is something else interfering. The code is correct and in the right place. Even without all other code in the .htaccess file it behaves the same way. I went to my own site as a test and installed Total Cache and I still was able to get it to work as described. It is likely a theme or plugin that is causing the interference. As a test, you may want to switch to another theme and see if it works then. If not, then it may be a plugin. You would want to turn them all off and see if it works then. If so, then enable them one by one and test in between. Once you enable the offending plugin the 404 redirect will break. Then you can decide which feature is better to keep, the plugin or the 404. To have the 404 you will likely need to get rid of that plugin.

Kindest Regards,
Scott M
n/a Points
2014-08-08 12:15 pm
Man you just nailed it in the head,been struggling with this and i'm sorted after bumping into your post.
My hosting company were not able to help me on this.
Thanks a milli.
n/a Points
2014-09-02 11:40 am
Introduce "your cPanel"
Staff
9,260 Points
2014-09-05 3:40 pm
Hello Virgil,

Thank you for contacting us today. If you are interested in learning about cPanel, see our guide titled What is cPanel.

Thank you,

John-Paul
n/a Points
2014-09-08 5:09 am

great stuff. Thanks

n/a Points
2014-09-27 2:36 am

we have problem with our domain, that is we can send a mail from other domain, but we are not able to send a mail with our domain. it showing error like this...

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() inC:\Inetpub\vhosts\healthheal.in\httpdocs\form.php on line 55

Staff
19,241 Points
2014-09-28 8:41 am
Hello Pallavi,

This is not the correct article for this type of question, you may want to create a new question as this is for discussion on setting a 404 page.

To get you started, however, do you have mail services set up on your local machine? The form is looking for 'localhost' which means it expects to use whatever machine the script is running from.

Again, please reply with a new question.

Kindest Regards,
Scott M

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

42 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!