电脑技术学习

wordpress 3.1引入Canonical URL Redirection技术

dn001

关于wordpress 3.1升级至首页重定向循环原因可能是:wp-include/template-loader.php文件头部的如下代码导致的。
if ( defined(‘WP_USE_THEMES’) && WP_USE_THEMES )
do_action(‘template_redirect’);
解决办法就是将这段的代码注释掉,首页可以正常访问了。

关于3.1的重定向:
为什么wordpress要加以上代码让所有页面都做一次模板判断跳转?Turn Off WordPress Homepage URL Redirection给出了答案,是wordpress针对一个页面存在多个网址的SEO优化。比如

http://**.net

http://**.net/index.php

http://www.**.net

http://www.**.net/index.php

上面这几个网址都是指向博客首页,但对于搜索引擎来说是存在相同页面内容的多个URL,不利于SEO。于是wordpress引入了Canonical URL Redirection技术,将多个网址重定向到其中一个网址上,也便是上面所看到代码的功能。实际起作用的代码位于wp-includes/canonical.php的redirect_canonical()函数中,有兴趣的同学可认真研究一下。

扩展:Canonical URL Redirection技术:
If you are integrating WordPress into a preexisting site that already has its own homepage, or if you are developing a new WordPress website that is hidden behind a Coming Soon page, you will run into one frustrating problem. If you try to access the WordPress installation by visiting the index.php file, you won’t be able to see it. Instead, WordPress will automatically redirect you from the index.php page to the blog address url, as defined in your WordPress site settings.

There are, of course, several ways to get around this problem. Two common ways include:
1.developing the WordPress site in its own directory
2.integrating the preexisting site or coming soon page within the WordPress site

Unfortunately, these methods can result in quite a bit of extra work. Fortunately, it is easy to stop WordPress’ automatic redirects. But first, it is a good idea to know why WordPress employs these redirects.

Why WordPress Has Automatic URL Redirects

A page on any website, WordPress or not, can be accessed by multiple urls. For example, you can typically visit the home page of a WordPress web site by all of the following urls:
•http://example.com/
•http://www.example.com/
•http://example.com/index.php
•http://www.example.com/index.php

The problem with allowing all of these ways to access a single page is that it can potentially hurt your website’s overall search engine optimization (SEO). Having multiple urls for a page means that search engines could index duplicate copies. So WordPress fixes this problem by employing automatic redirects known as Canonical URL Redirection, which only enables one url per page.