如何重定向网站上不存在的链接回到首页


how to redirect non existent link on website back to the home page

我的问题是如何重定向任何不存在的链接在我的网站回到主页。例如;假设我的站点(http://mywebsite.com/pets)上有一个名为"pets"的页面,如果用户不小心输入http://mywebsite.com/petsd,我希望将用户重定向到主页。有人知道怎么做吗?谢谢。

在。htaccess文件中设置ErrorDocument 404 /index.html

为404页面添加javascript重定向。确保是定时的,这样访问者可以看到发生了什么。

您可以使用mod_rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [R]

使用mod_rewrite,你可以处理404(未找到的页面),像这样:

RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . / [L]

这将在浏览器中保留原始链接,但将显示您的主页。