How to implement pseudo-static adding html suffix in WordPress
WordPress如何实现伪静态加html后缀?本文给大家分享的是在使用WordPress的过程中,发现其内容页分页的形式非常的不友好,于是就动手进行修改的过程以及详细代码,非常的实用,推荐给大家。希望对大家有所帮助。
问题
当文章有分页的时候,WordPress生成的永久链接是page.html/2的形式,前面一段是文章的$link,后面的数字代表分页$number。那么问题来了,挖掘……不对,从逻辑上讲这到底是个html文件还是一个目录呢?
难看
在.html这个静态文件后面加上一个/和数字简直令人摸不着头脑,这还只是其次,重要的是,我发现搜索引擎根本不收录这样奇怪的链接,这个真是无法接受,我写的东西首尾都很重要,不能因为分个页就被忽视了。
不收录
拿这篇文章来看,搜索文章第一页的内容,谷歌收录了:
搜索文章第二页和第三页的内容,根本没有收录:
解决方案
于是我决定DIY WordPress的链接生成与解析规则。
思路
利用filter wp_link_pages_link 将分页链接/123456重写为page-[123456].html。
利用WordPress或者服务器的RewriteRule将page-[123456].html还原为/123456
添加钩子redirect_canonical,防止WordPress从page-[123456].html到/123456的强行跳转。
生成分页html后缀链接
给WordPress主题加入:
class Rewrite_Inner_Page_Links { var $separator; var $post_rule; function __construct() { $this->separator = '/page-'; // (.+?)/([^/]+).html(/[0-9]+)?/? $this->post_rule = '(.+?)/([^/]+)(' . $this->separator . '([0-9]+))+.html/?$'; if (!is_admin() || defined('DOING_AJAX')) : add_filter('wp_link_pages_link', array($this, 'inner_page_link_format'), 10, 2); // for inner pages add_filter('redirect_canonical', array($this, 'cancel_redirect_for_paged_posts'), 10, 2); endif; if (is_admin()) : add_filter('rewrite_rules_array', array($this, 'pagelink_rewrite_rules')); endif; } /** * 修改post分页链接的格式 * @param string $link * @param int $number * @return string */ function inner_page_link_format($link, $number) { if ($number > 1) { if (preg_match('%<a href=".*\.html/\d*"%', $link)) { $link = preg_replace("%(\.html)/(\d*)%", $this->separator . "$2$1", $link); } } return $link; } /** * 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录 * * 访问原始链接将返回404 * @param array $rules * @return array */ function pagelink_rewrite_rules($rules) { $new_rule[$this->post_rule] = 'index.php?name=$matches[2]&page=$matches[4]'; return $new_rule + $rules; } /** * 禁止WordPress将页面分页链接跳转到原来的格式 * @param string $redirect_url * @param string $requested_url * @return bool */ function cancel_redirect_for_paged_posts($redirect_url, $requested_url) { global $wp_query; if (is_single() && $wp_query->get('page') > 1) { return false; } return true; } } new Rewrite_Inner_Page_Links();
这样就得到了将类似http://www.***.com/program/tokyodaigaku.html/2/ 的分页链接转化为形如 http://www.***.com/program/tokyodaigaku/page-2.html 的链接。
注意,我的伪静态规则是/%category%/%postname%.html,如果你的规则不同,请自行修改代码或者伪静态规则。
重写URL规则
如果不重写规则的话,WordPress是不认识这个链接的,它以为有个目录叫tokyodaigaku,里面有篇文章叫page-2.html,结果会给出一个无情的404错误:
利用服务器的重写规则
如果是SAE的话,在config.yaml的第一行加入:
复制代码 代码如下:
- rewrite: if ( !is_dir() && path ~ "(.+?)/([^/]+)(/page-([0-9]+))+.html/?$") goto "index.php?name=$2&page=$4"
如果不是的话,可以利用WordPress自带的rewrite_rules:
登陆后台——设置——固定链接:
什么也不用填,直接保存更改即可。代码会自动在数据库中硬性加入一条规则:
复制代码 代码如下:
"(.+?)/([^/]+)(/page-([0-9]+))+.html/?$" => "index.php?name=$matches[2]&page=$matches[4]"
最终效果
无论是用http://www.***.com/program/tokyodaigaku.html/2/ 还是 http://www.***.com/program/tokyodaigaku/page-2.html,都可以访问第二页。
具体效果放在第二页,顺便测试一下分页后缀效果
好了,基本功能已经实现了,小伙伴们是否明白了呢,如有问题,可以留言探讨哦!
相关推荐:
The above is the detailed content of How to implement pseudo-static adding html suffix in WordPress. For more information, please follow other related articles on the PHP Chinese website!

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool