How do I make .htaccess for 404?

2

How do I make a .htaccess so that when it detects a 404 error redirect itself, that is to the index.html.

I already have the file and I uploaded it to the page in the route / public_html, but I do not know what to put it in for it to work.

I already tried with: ErrorDocument 404 http://yoyomero.com But it does not work

    
asked by Jose Fabio 웃 18.12.2016 в 20:24
source

2 answers

9

In the .htaccess file you should create a code similar to this:

RewriteEngine On
ErrorDocument 404 /404.php
  

Note: There are many options and different structure types and different ways to redirect to a specific error page either using .htaccess or PHP strong> natively among others.

Somewhat more advanced to the different redirection errors.

ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]

RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]

# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]

The .htaccess of the previous example has several examples in it. You can use the following as the generic redirect script to replace 404_redirect.php previously.

error_redirect.php

<?php
   $error_url = $_SERVER["REDIRECT_STATUS"] . '/';
   $error_path = $error_url . '.php';

   if ( ! file_exists($error_path)) {
      // this is the default error if a specific error page is not found
      $error_url = '404/';
   }

   header('Location: ' . $error_url);
   exit;
?>

Source SO English

    
answered by 18.12.2016 / 21:57
source
0
ErrorDocument 404 http://www.yoyomero.com

That was the solution, when I uploaded the .htaccess file the 2 were saved instead of just one, the first one was .htaccess that was empty, then I uploaded the other one and it was renamed to .htaccess_ , that's why it did not work.

    
answered by 18.12.2016 в 23:12