|
|
原文链接 http://advertcn.com/viewthread.p ... 26amp%3Btypeid%3D96
Alright so a few days ago I successfully purchased an expiring domain from an auction site, and after wasting a few hours doing 301 redirects manually (monitoring 404s and adding redirects by hand in .htaccess file) I accidently found this super easy method to 301 redirect all 404 error pages to root domain.
几天前在拍卖站买到一个将要过期的域名。在几个小时的手工设置301跳转(检查404错误页面然后再.htaccess文件添加跳转)后,我发现了一个超级简单的用301跳转所有404错误页面到网站首页的办法。
This trick was simply done by a WP core function called wp_redirect(). Here are the parameters for the function:
这个技巧的实现是wp_redirect()这个主函数。下面是这个函数的参数:- $location
- (string) (required) The absolute URI which the user will be redirected to.
- Default: None
- $status
- (integer) (optional) The status code to use.
- Default: 302
复制代码 Here's how the function should be used:
这个是该函数的使用方法:- <?php
- wp_redirect( $location, $status );
- exit;
- ?>
复制代码 Here's a real life example 这个是实际的例子- <?php wp_redirect( get_option( 'siteurl' ); exit; ?>
- What this does is, calling the get_option('siteurl') will basically return the default domain name that you're using (One that you've set in the Settings > General tab). And then, redirects it using a 302 since we haven't exclusively specified that we need a 301 redirect (302 is the default).
复制代码 But.. this isn't what we want, we need it to redirect with a 301, right?
但这些并不是我们想要的,我们需要用301跳转。- <?php wp_redirect(get_option('siteurl'),301); ?>
- Yep, right there. This basically gets the default domain name and makes the redirect with a 301.
复制代码 So how to redirect all error pages to default domain name (root domain) with a 301?
Simple, create a file and name it 404.php and paste the above code in it, then replace your existing 404.php with it.
Remember that you need to replace the 404.php in your theme folder, which is located at wp-content > themes > your theme.
Enjoy~
怎么用301跳转所有的错误页面到网站首页呢?
很简单,创建一个名称为404.php的文件,复制上面的代理到该文件,然后替代wp中的404.php文件。
注意一点是 你需要在你的wp主题文件夹中替换掉原来的404.php文件,这个文件位于 wp-content > themes > your theme |
|