简体   繁体   English

重定向到主目录中的自定义404页面

[英]Redirecting to custom 404 page in home directory

I am having trouble redirecting the 404 (incorrect url requests) to a 404 page (404.php) I developed and uploaded to the server. 我无法将404(错误的url请求)重定向到我开发并上传到服务器的404页面(404.php)。

The server has a Wordpress installation in the sub-folder /blog and all the 404 are getting redirected to wordpress blog's 404 page. 该服务器在/ blog子文件夹中安装了Wordpress,所有404都已重定向到wordpress博客的404页面。 I have set the redirection in .htaccess but is not working. 我已经在.htaccess中设置了重定向,但无法正常工作。

ErrorDocument 404 /404.php

This is what I added in .htaccess, but it still redirects to the blog's 404 instead of the 404 page in root. 这是我在.htaccess中添加的内容,但仍会重定向到博客的404而不是根目录下的404页面。

How can I override this? 如何覆盖呢?

In my understanding ErrorDocument 404 /404.php will only work if you have not specified rewrite rule in .htaccess eg: 据我了解, ErrorDocument 404 /404.php仅在未在.htaccess指定重写规则的情况下才有效,例如:

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

Because wordpress has it's own life cycle which uses URL rewrite in .htaccess to determine which page request are coming. 因为wordpress有它自己的生命周期 ,所以使用.htaccess URL重写来确定哪个页面请求即将到来。 WordPress has it's core mechanism to understand the current request. WordPress具有了解当前请求的核心机制。 For example you have created a page name hello-word , when you go to http://your_wordpress.com/hello-word wordpress determines hello-world from URL and find in it's database whether it has hello-word page or not. 例如,您创建了一个页面名称hello-word ,当您转到http://your_wordpress.com/hello-word wordpress从URL确定hello-world并在其数据库中查找是否具有hello-word页面。 If current page does not exist in database then it's mechanism automatically call theme 404 template which is located in currently active theme. 如果数据库中不存在当前页面,则其机制会自动调用位于当前活动主题中的主题404模板。

Solution

Create a file in wp-content/themes/YOUR-THEME/404.php and change it's content. wp-content/themes/YOUR-THEME/404.php创建一个文件并更改其内容。 You don't need to specify ErrorDocument 404 /404.php in .htaccess . 您无需在.htaccess指定ErrorDocument 404 /404.php Read more how to create 404 template here 在此处详细了解如何创建404模板

I achieved it by pasting this code on top of my wordpress theme's header.php file 我通过将这段代码粘贴到我的wordpress主题的header.php文件顶部来实现此目的

 if ( is_404() ) {
      wp_redirect( '../404.php' );
      exit;
   }

And now the root/main site 404 page is getting loaded instead of wordpress blog's 404 page as intended. 现在,根/主站点404页面已加载,而不是按预期加载了wordpress博客的404页面。 :) :)

first of all make sure that the 404.php is present in the proper location and http://www.example.com/404.php is working properly. 首先,请确保404.php位于正确的位置,并且http://www.example.com/404.php正常运行。

if both 404.php and .htaccess are present in the same root folder, then try changing following line in .htaccess file 如果404.php和.htaccess都存在于同一根文件夹中,请尝试更改.htaccess文件中的以下行

ErrorDocument 404 /404.php 错误文档404 /404.php

to

ErrorDocument 404 /root/404.php ErrorDocument 404 /root/404.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM