如何使用.htaccess将错误404重定向到主页?

时间:2022-10-07 00:23:38

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

您好我如何使用.htaccess将错误404重定向到主页?

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

示例:site.com如果写入site.com/some_site_notforund而不是404将我们重定向到主页面

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

示例2:sadistic.pl如果写sadistic.pl/some_site_notfound而不是404将我们重定向到当前页面

5 个解决方案

#1


7  

Try:

尝试:

FallbackResource /index.html

or whatever the homepage is

或者无论主页是什么


try:

尝试:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

#2


3  

You can do this like

你可以这样做

this will be .htaccess file:

这将是.htaccess文件:

ErrorDocument 404 /404.php

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

所有未找到页面的页面都将显示404.php文件。

In 404.php file you can write:

在404.php文件中你可以写:

<?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

因此,所有未找到页面的页面都将转到404.php页面,它会将它们重定向到index.php

#3


2  

  1. Make a .htaccess file in your root directory.
  2. 在根目录中创建.htaccess文件。
  3. Put this code into it and customize the RewriteRule.
  4. 将此代码放入其中并自定义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.

在我的localhost上完美运行。

#4


1  

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

它适用于我的网站。我试过这段代码,它没有显示404页面,但它显示的是相同的URL,但主页的内容

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

#5


1  

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

或者保存重新路由步骤,只需将以下内容放入.htaccess文件中:

ErrorDocument 404 /index.php

ErrorDocument 404 /index.php

#1


7  

Try:

尝试:

FallbackResource /index.html

or whatever the homepage is

或者无论主页是什么


try:

尝试:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

#2


3  

You can do this like

你可以这样做

this will be .htaccess file:

这将是.htaccess文件:

ErrorDocument 404 /404.php

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

所有未找到页面的页面都将显示404.php文件。

In 404.php file you can write:

在404.php文件中你可以写:

<?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

因此,所有未找到页面的页面都将转到404.php页面,它会将它们重定向到index.php

#3


2  

  1. Make a .htaccess file in your root directory.
  2. 在根目录中创建.htaccess文件。
  3. Put this code into it and customize the RewriteRule.
  4. 将此代码放入其中并自定义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.

在我的localhost上完美运行。

#4


1  

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

它适用于我的网站。我试过这段代码,它没有显示404页面,但它显示的是相同的URL,但主页的内容

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

#5


1  

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

或者保存重新路由步骤,只需将以下内容放入.htaccess文件中:

ErrorDocument 404 /index.php

ErrorDocument 404 /index.php