您的位置:首页 > 编程语言 > PHP开发

wordpress内核揭秘之day6_Permalink伪静态方法实现原理

2016-12-22 11:52 519 查看
这两天百度到这么一些说法,“wordpress是通过apache转义 rewrite_rule转换成wordpress能识别的url”;

一眼看上去,貌似也说得挺专业的,

但以我的经验看,提了多过于3个专业词汇(apache, rewrite_rul,url, ...),反而是不专业的,是的,这句话里面有多个专业词汇

分析一下

说这话的人

Wordpress的创造者

经常百度国外开源组织(人数不明)

程序猿一个靠donate吃饭,日常没事干的歪果仁

希望一句话概括抽象                               开发php超过10年

 组织结构不明,但估计严密程度某国家政党

 可能是w3c标准的制定者

就这么一个人说的1句话能比得过一个组织?想想就不合理,爱因斯坦相对论厉害的地方并不在于他证明了相对论有多屌(至今没有能证明相对论是对的),原子弹还是会相对论的其中小部分,相对论屌的地方在于没人能证明他不合理的地方,所以他是对的

就这么一个人说的一句话希望能概括一个组织做的事情,显然不合理(其实这个人是想概括2个组织),看出来了么?

“wordpress是通过apache转义 rewrite_rule转换成wordpress能识别的url”

这么一个人想把apache也概括进来。。。。。

我艹,所以这么一个人说的东西有多准确你懂了吧

我说的也是个人说的,所以也不要相信我说的,最好还是自己去摸索

我们今天解密内容的前提就是

1.这个人说的是错的,而且大错特错

2.由于第一点,所以wordpress 源码里面有很屌的,完善的permaklilnk伪静态机制,也就是rewrite机制

3.由于第二点,我们就从源码看起

permaklink的源码在:

wp-admin/options-permarklink.php

里面有一句代码

flush_rewrite_rules();

只是看名字你就应该知道了吧,前文说的,一开始说的,“这么一个人”说的就是错的,因为wordpress里面包含rewrite_rules。。。。。。。

options-permarklink.php里面的代码,也是提交的实现代码

(整个options-permarklink.php代码很多,很乱,如果你做过2年程序员你可能会同意这种说法, 至于怎么找到的就不在这里详细说了,就在options-permarklink.php里面,很简单而且不用去其他继承,基类里面找,个人还是比较喜欢php,这种超链接的服务器交互方法,比什么框架都强多了)

if ( isset( $_POST['category_base'] ) ) {
$category_base = $_POST['category_base'];
if ( ! empty( $category_base ) )
$category_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
$wp_rewrite->set_category_base( $category_base );
}

if ( isset( $_POST['tag_base'] ) ) {
$tag_base = $_POST['tag_base'];
if ( ! empty( $tag_base ) )
$tag_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
$wp_rewrite->set_tag_base( $tag_base );
}

$message = __( 'Permalink structure updated.' );


虽然光看这么个代码,你可能还是不肯定wordpress是数据库还是配置文件去处理这个伪静态的(实际上,大部分组织都是用config等去处理的),但我们现在还不100%肯定wordpress是这么做,我们要找到里面真实实现的源码确认,但看到上面这么个代码,比较能确定的是,wordpress里面用$wp_rewrite去处理这个伪静态,

所以wordpress里面是有伪静态处理机制的(文章开头那么一个人又证明是错了)

我们找一找:

/**
* Sets the main permalink structure for the site.
*
* Will update the 'permalink_structure' option, if there is a difference
* between the current permalink structure and the parameter value. Calls
* WP_Rewrite::init() after the option is updated.
*
* Fires the {@see 'permalink_structure_changed'} action once the init call has
* processed passing the old and new values
*
* @since 1.5.0
* @access public
*
* @param string $permalink_structure Permalink structure.
*/
public function set_permalink_structure($permalink_structure) {
if ( $permalink_structure != $this->permalink_structure ) {
$old_permalink_structure = $this->permalink_structure;
update_option('permalink_structure', $permalink_structure);

$this->init();

/**
* Fires after the permalink structure is updated.
*
* @since 2.8.0
*
* @param string $old_permalink_structure The previous permalink structure.
* @param string $permalink_structure     The new permalink structure.
*/
do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure );
}
}


说揭秘也有点不好意思,人家源码都已经说的这么明白了

1)1.5.0版本开始有了这个伪静态结构(2006年,刚好10年前),整个wordpress伪静态的配置实现就在update_option()

2)2.8.0版本开始,当你提交后,会马上更新整个站点的url rewrite structure.......

2.8.0版本的 action : permalink_structure_changed反而是我们比较需要了解的,所以update_option先放一放

也有网上传言写成.html会比较好收录,为乐方便运营,建站前要想好架构等等言论

(这些言论也是很天真的一种言论,运营过程中,通过rewrite改变网站结构以防黑客,骗搜索引擎,也应该是很普通的编程技巧吧。。。。。说这些言论的人明显不是程序猿,或者我一直所说的,她们根本就不适合做程序,她们可能也不至于一无是处,但你要选择哪种理论,往哪个方向那是你做程序猿所必需的选择)

静态页面的解决方案在wordpress就是写成/%post_id%.html,如下图:



其实如果百度不是很变态,伪静态也足够应付引擎(事实上,/%post_id%.html就还是一个伪静态,并不是生成真的静态页面,phpcms,织梦cms在静态页面模块做得更符合国内搜索引擎)

Day and name 这样的伪静态,2者是相等的,只要百度引擎正常点,应该能识别
http://localhost:8888/2016/12/28/sample-post/  ==
http://localhost:8888/2016/12/28/sample-post/index.html
但我的网站确实还没被百度收录,不确定什么原因
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php wordpress wp 博客 源码