Solved

create 404 redirect in .htaccess

Posted on 2011-03-19
5
317 Views
Last Modified: 2012-05-11
how would i create a 404 redirect to my home page in a .htaccess file?
0
Comment
Question by:COwebmaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 333 total points
ID: 35173374
Put this in your .htaccess file:

ErrorDocument 404 /index.html

Open in new window


This will tell your server to redirect to index.html when no page can be found.
0
 

Author Comment

by:COwebmaster
ID: 35173414
tried that but still I get a 404 error.  I have a wordpress site and in my htaccess file I have:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
ErrorDocument 404 /index.php
</IfModule>

0
 
LVL 8

Assisted Solution

by:Rik-Legger
Rik-Legger earned 333 total points
ID: 35173441
I suppose that you get the 404 error from wordpress?
Because looking at your .htaccess everything (even pages that don't exist) will be 'send' to index.php.
If not, maybe you can attach a screenshot so i can see whats going on?
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 35173581
I can show you how I handle the 404 issue in PHP (not .htaccess).  This specifically addresses the issue that my clients seem to have a propensity to write URLs like /results instead of /results.php
<?php // 404handler.php
error_reporting(E_ALL);

if (isset($_SERVER["REQUEST_URI"]))
{
    // IF A PHP SCRIPT IS NOT FOUND GO TO THE HOME PAGE
    if (preg_match('#\.php#i', $_SERVER["REQUEST_URI"]))
    {
        header('Location: /');
        exit;
    }

    // IF A NON-PHP SCRIPT IS NOT FOUND, TRY IT WITH PHP
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $arr[0] .= '.php';
    $uri = implode('?', $arr);
    header("Location: $uri");
    exit;
}

// IF NO REQUEST URI
header('Location: /');
exit;

Open in new window

0
 

Author Closing Comment

by:COwebmaster
ID: 35326673
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchiā€¦

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question