


WordPress paging pseudo-static plus html suffix, wordpress_PHP tutorial
Jul 12, 2016 am 08:50 AMWordPress分頁(yè)偽靜態(tài)加html后綴,wordpress
問(wèn)題
當(dāng)文章有分頁(yè)的時(shí)候,WordPress生成的永久鏈接是page.html/2的形式,前面一段是文章的$link,后面的數(shù)字代表分頁(yè)$number。那么問(wèn)題來(lái)了,挖掘……不對(duì),從邏輯上講這到底是個(gè)html文件還是一個(gè)目錄呢?
難看
在.html這個(gè)靜態(tài)文件后面加上一個(gè)/和數(shù)字簡(jiǎn)直令人摸不著頭腦,這還只是其次,重要的是,我發(fā)現(xiàn)搜索引擎根本不收錄這樣奇怪的鏈接,這個(gè)真是無(wú)法接受,我寫(xiě)的東西首尾都很重要,不能因?yàn)榉謧€(gè)頁(yè)就被忽視了。
不收錄
拿這篇文章來(lái)看,搜索文章第一頁(yè)的內(nèi)容,谷歌收錄了:
搜索文章第二頁(yè)和第三頁(yè)的內(nèi)容,根本沒(méi)有收錄:
解決方案
于是我決定DIY WordPress的鏈接生成與解析規(guī)則。
思路
利用filter wp_link_pages_link 將分頁(yè)鏈接/123456重寫(xiě)為page-[123456].html。
利用WordPress或者服務(wù)器的RewriteRule將page-[123456].html還原為/123456
添加鉤子redirect_canonical,防止WordPress從page-[123456].html到/123456的強(qiáng)行跳轉(zhuǎn)。
生成分頁(yè)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分頁(yè)鏈接的格式 * @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; } /** * 為新的鏈接格式增加重定向規(guī)則,移除原始分頁(yè)鏈接的重定向規(guī)則,防止重復(fù)收錄 * * 訪問(wèn)原始鏈接將返回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將頁(yè)面分頁(yè)鏈接跳轉(zhuǎn)到原來(lái)的格式 * @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.domain.com/program/tokyodaigaku.html/2/ 的分頁(yè)鏈接轉(zhuǎn)化為形如 http://www.domain.com/program/tokyodaigaku/page-2.html 的鏈接。
注意,我的偽靜態(tài)規(guī)則是/%category%/%postname%.html,如果你的規(guī)則不同,請(qǐng)自行修改代碼或者偽靜態(tài)規(guī)則。
重寫(xiě)URL規(guī)則
如果不重寫(xiě)規(guī)則的話,WordPress是不認(rèn)識(shí)這個(gè)鏈接的,它以為有個(gè)目錄叫tokyodaigaku,里面有篇文章叫page-2.html,結(jié)果會(huì)給出一個(gè)無(wú)情的404錯(cuò)誤:
利用服務(wù)器的重寫(xiě)規(guī)則
如果是SAE的話,在config.yaml的第一行加入:
復(fù)制代碼 代碼如下:- rewrite: if ( !is_dir() && path ~ "(.+?)/([^/]+)(/page-([0-9]+))+.html/?$") goto "index.php?name=$2&page=$4"
如果不是的話,可以利用WordPress自帶的rewrite_rules:
登陸后臺(tái)——設(shè)置——固定鏈接:
什么也不用填,直接保存更改即可。代碼會(huì)自動(dòng)在數(shù)據(jù)庫(kù)中硬性加入一條規(guī)則:
復(fù)制代碼 代碼如下:"(.+?)/([^/]+)(/page-([0-9]+))+.html/?$" => "index.php?name=$matches[2]&page=$matches[4]"
最終效果
無(wú)論是用http://www.domain.com/program/tokyodaigaku.html/2/ 還是 http://www.domain.com/program/tokyodaigaku/page-2.html,都可以訪問(wèn)第二頁(yè)。
具體效果放在第二頁(yè),順便測(cè)試一下分頁(yè)后綴效果
好了,基本功能已經(jīng)實(shí)現(xiàn)了,小伙伴們是否明白了呢,如有問(wèn)題,請(qǐng)留言

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.
