Home Backend Development PHP Tutorial How to implement pseudo-static adding html suffix in WordPress

How to implement pseudo-static adding html suffix in WordPress

Jan 02, 2018 am 11:08 AM
html wordpress static

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(&#39;%<a href=".*\.html/\d*"%&#39;, $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] = &#39;index.php?name=$matches[2]&page=$matches[4]&#39;;
    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(&#39;page&#39;) > 1)
    {
      return false;
    }
    return true;
  }
}
 
new Rewrite_Inner_Page_Links();
Copy after login

这样就得到了将类似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,都可以访问第二页。

具体效果放在第二页,顺便测试一下分页后缀效果

好了,基本功能已经实现了,小伙伴们是否明白了呢,如有问题,可以留言探讨哦!

相关推荐:

解析WordPress中hook的使用

给WordPress中的留言加上楼层号

总结关于WordPress注意点

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

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.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

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.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

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.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

See all articles