How to redirect an error 404 to home page with .ht

2020-02-28 12:16发布

Hello how do I redirect an error 404 to a home page with .htaccess?

Example: site.com if write site.com/some_site_notforund instead of 404 redirects us to the main page

Example 2:
sadistic.pl if write sadistic.pl/some_site_notfound instead of 404 redirects us to current page

5条回答
劳资没心,怎么记你
2楼-- · 2020-02-28 12:56

Try:

FallbackResource /index.html

or whatever the homepage is


try:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
查看更多
贼婆χ
3楼-- · 2020-02-28 12:57

It works fine for my site. I tried this code, it doesn't show 404 pages, but it shows the same URL but content of the homepage

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
查看更多
萌系小妹纸
4楼-- · 2020-02-28 13:00

You can do this like

this will be .htaccess file:

ErrorDocument 404 /404.php

All the page not found pages will display 404.php file.

In 404.php file you can write:

<?php
    header('location:index.php');
?>

So all page not found pages will go to 404.php page and it will redirect them to index.php

查看更多
女痞
5楼-- · 2020-02-28 13:02
  1. Make a .htaccess file in your root directory.
  2. Put this code into it and customize the RewriteRule.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ http://127.0.0.1/dir/index.php [L]

Works perfectly on my localhost.

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-28 13:22

Or save a reroute step and just put the following into your .htaccess file:

ErrorDocument 404 /index.php

查看更多
登录 后发表回答