开发者

In what way does Wordpress rewrite page URLs?

开发者 https://www.devze.com 2022-12-28 14:33 出处:网络
Recently I\'m interested in post\'s structure of Wordpress. They use a table named (wp_posts) and in this table they saved 3 related fields such as :

Recently I'm interested in post's structure of Wordpress.

They use a table named (wp_posts) and in this table they saved 3 related fields such as :

post_title
post_name
guid 

It's clear that they save title of each story in post_title field , and slugs in post_name , and full url of a post in guild filed .

But where the hell, they rewrite these urls in way it appears in browsers :

http://localhost/wordpress/about/

There is no htaccess rules for this !

I checked rewrite.php and did开发者_如何学Pythonn't understand an inch ?!

i need to create similar pages , what steps should i take !?


The .htaccess file has a rewrite directive that sends all requests to index.php. The rewrite directive tells the web server to pass the original request to a different location without redirecting. So, index.php receives all the original parameters, including the request path (the part of the URL after the hostname, e.g., "/about/").

When index.php receives a request, it acts like a front controller, figuring out how to respond based on the URL.

I never looked at the inner workings of WordPress so I can't say exactly how they implemented it, but the general idea for index.php is this:

  1. Look at the request path (e.g., "/about/") that the client used
  2. Extract a slug from the request path ("about")
  3. Look up which post has the slug "about"
  4. Return the appropriate post


A lot of your questions will be explained if you examine the WP_Rewrite class.

Basically, as most of you have said, the .htaccess merely rewrites all URLs that do not resolve to an actual file or folder on the server to index.php.

WordPress maps the URL against a list of rewrite rules, which is an array of keys and values. The key is a regular expression, and the value maps back references to a parameter string.

For example, one rewrite rule is;

category/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&paged=$matches[2]'

Then the class WP along with WP_Query take the parameters and handle the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号