WordPress blog category page 404 error resolution (URL static)

Source: Internet
Author: User
Tags auth json php file wordpress blog

Vacation nothing, and again to their own website (6th station) on-line, abandoned the original DokuWiki (advantages: Simple deployment, no database support, UI concise; disadvantage: The article Management logic peculiar, need handwritten markdown), and then proud of the use of WordPress, fix the theme, After a makeover, found something in the ointment ——— ugly url, paste a few people feel:

Sixther.me/?p=85 #特定文章的url, according to post_id query
sixther.me/?m=201512 #查看特定日期的文章, query based on date string
Sixther.me/?category=15 #查看特定类别的文章, according to catetory_id query
Sixther.me/?tag=monitor #查看特定标签的文章, according to Tag_string query
Sixther.me/?author=1 #查看特定作者的文章, according to author_id query
Note: These formulations are omitted index.php, the complete formulation should be:
Https://sixther.me/index.php?author=1
URL is also part of the UI, should also be simple and friendly, at least let the Web crawler know, the first step, the first use of WordPress with the fixed link settings to static it, so set up, the above URL becomes the following:
Fixed Link: https://sixther.me/%postname%.html

Sixther.me/vim-template-for-python-file.html
Sixther.me/2015/12
Sixther.me/category/linux
Sixther.me/tag/python
Sixther.me/author/sixther
Yes, it should be reasonable, also should be the ultimate goal, but this time there are big problems, all the links, including articles, categories, dates, tags, the author has become 404 pages, do not know this is not a WordPress bug, fixed link function should not be this way, Maybe it's related to the version, The 6th station is a 4.4 version, that is to say, the current site does not know these elegant URLs, only to know the above ugly URLs, this situation can be solved by nginx, fine thinking, also can not be completely resolved, because the elegant URL inside is the name attribute, ugly URL is the id attribute of things, only Tag and date can be done with rewrite, first plus:

Rewrite ^/([0-9]+)/([0-9]+)/index.php?m=$1$2 last;
Rewrite ^/tag/(. *)/index.php?tag=$1 last;
bingo!!
To consider the remaining few, obviously, there are two solutions, One is to find index.php through the name of the query results of the parameters, the other is to allow the program to generate HREF when the ID to generate, that can only go to see the WordPress code, the first notice of the auth-template.php file has a get_author_ Posts_url function:

function Get_author_posts_url ($author _id, $author _nicename = ') {
Global $WP _rewrite;
$auth _id = (int) $author _id;
$link = $wp _rewrite->get_author_permastruct ();
if (empty ($link)) {
$file = Home_url ('/');
$link = $file. ' Author= '. $auth _id;
} else {
if (' = = $author _nicename) {
$user = Get_userdata ($author _id);
if (!empty ($user->user_nicename))
$author _nicename = $user->user_nicename;
}
$link = Str_replace ('%author% ', $author _nicename, $link);
$link = Str_replace ('%author% ', $author _id, $link);
$link = Home_url (User_trailingslashit ($link));
}
$link = apply_filters (' Author_link ', $link, $author _id, $author _nicename);
return $link;
}
Note that the line I was going to change, and later it turns out that this can be done according to author ID query, you can see that the way to generate Author_posts_url is to replace%author% with $author_nicename, Some of the basic MVC students will understand that%author% is a template variable, that category,post should also do it, with "%category% && str_replace" Keyword search category-template.php file, but there is no similar code, really do not understand WordPress code logic, have the opportunity must be combed, the second way to catch up here did not go down.
Then is a variety of Google, the basic key word is "wordpress api,wordpress category 404 ..." And so on, and then found the query.php file, the first line of open file let me see the hope of solving the problem (is a line of comments):

/**
* WordPress Query API
*
* The query API attempts to get which part of WordPress, the user is on. It
* Also provides functionality for getting URL query information.
*
Looking down, it's like discovering a new continent:

Public Function Fill_query_vars ($array) {
$keys = Array (
' Error '
, ' m '
, ' P '
, ' Post_parent '
, ' Subpost '
, ' subpost_id '
, ' attachment '
, ' attachment_id '
, ' name '
, ' Static '
, ' PAGENAME '
, ' page_id '
, ' second '
, ' minute '
, ' Hour '
, ' Day '
, ' Monthnum '
, ' Year '
, ' W '
, ' Category_name '
, ' tag '
, ' Cat '
, ' tag_id '
, ' author '
, ' Author_name '
, ' feed '
, ' TB '
, ' paged '
, ' Comments_popup '
, ' Meta_key '
, ' Meta_value '
, ' preview '
, ' s '
, ' sentence '
, ' title '
, ' Fields '
, ' Menu_order '
);
Apply Langya list in a sentence: "The woman's feeling is so unreasonable", at the moment my feeling is hundred confirm these are index.php support query parameter list, no more scrutiny code, directly modify Nginx rewrite rules, Without modifying even one line of WordPress's code, the complete rules are as follows:

Rewrite ^/(. *). Html/index.php?name=$1 last;
Rewrite ^/([0-9]+)/([0-9]+)/index.php?m=$1$2 last;
Rewrite ^/tag/(. *)/index.php?tag=$1 last;
Rewrite ^/page/([0-9]+)/index.php?paged=$1 last;
Rewrite ^/author/(. *)/?author_name=$1 last;
Rewrite ^/category/(. *)/?category_name=$1 last;
All Bingo!!!
Eggs:
To check the problem of the process, found a call Wp-json things, you can query the corresponding content through JSON, if you want to develop WordPress app, this must use.

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.