WordPress page pseudo static add HTML suffix _php tips

Source: Internet
Author: User

Problem

When the article has pagination, the permanent link generated by WordPress is the form of PAGE.HTML/2, the previous paragraph is the $link of the article, the following number represents pagination $number. So here's the problem, digging ... No, logically, is this an HTML file or a directory?

Ugly

Adding A/And number to the back of the. html file is simply confusing. This is just the second, the important thing is, I found that the search engine does not include such a strange link, this is really unacceptable, I write things are very important at the end, can not be overlooked because of the paging.

Not included

Take this article to look at the content of the first page of the search article, Google included:

The contents of the second and third pages of the search articles are not included at all:

Solution

So I decided to DIY WordPress link generation and parsing rules.

Ideas

Use the filter wp_link_pages_link to rewrite the paging link/123456 as page-[123456].html.

Use WordPress or server rewriterule to restore page-[123456].html to/123456

Add hook redirect_canonical to prevent WordPress from page-[123456].html to/123456 forced jump.

Raw content page HTML suffix link

Add to WordPress theme:

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 /** * Modify Post Paging link format * @param string $link * @param int $number * @return string/function Inner_p Age_link_format ($link, $number) {if ($number > 1) {if Preg_match ('%<a href= '. *\.html/\d* '% ', $lin K) {$link = Preg_replace ("% (\.html)/(\d*)%", $this->separator.
      "$2$1", $link);
  }
    }  return $link;
   /** * Adds redirection rules for new link formats, removes redirect rules for original paging links, prevents duplicate collection * * Access to original link returns 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; /** * Disables WordPress to jump page pagination links to the original format * @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 ();

This allows you to turn page-like http://www.domain.com/program/tokyodaigaku.html/2/links into the form of http://www.domain.com/program/ Links to Tokyodaigaku/page-2.html.

Note that my pseudo static rule is/%category%/%postname%.html, and if your rules are different, modify the code or the pseudo static rules yourself.

overriding URL rules

If you do not rewrite the rules, WordPress is not aware of this link, it thought there is a directory called Tokyodaigaku, there is an article called page-2.html, the result will give a ruthless 404 error:

Leveraging the server's overriding rules

If the SAE is the case, join the first line in Config.yaml:

Copy Code code as follows:
-Rewrite:if (!is_dir () && Path ~ "(. +?) /([^/]+) (/page-([0-9]+)) +.html/?$ ") goto" index.php?name=$2&page=$4 "

If not, you can take advantage of WordPress's own rewrite_rules:

Login background--settings--Fixed link:

Do not fill in anything, save changes directly. The code automatically adds a rule to the database:

Copy Code code as follows:
"(.+?) /([^/]+) (/page-([0-9]+)) +.html/?$ "=>" index.php?name= $matches [2]&page= $matches [4] "

Final effect

You can access the second page, either with http://www.domain.com/program/tokyodaigaku.html/2/or http://www.domain.com/program/tokyodaigaku/page-2.html.

Put the effect on the second page and test the page suffix effect

Well, the basic function has been achieved, the small partners understand it, if there is a problem, please leave a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.