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:
- Look at the request path (e.g., "/about/") that the client used
- Extract a slug from the request path ("about")
- Look up which post has the slug "about"
- 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.
精彩评论