views:

121

answers:

2

Hey

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 didn't understand an inch ?!

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

+4  A: 

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
Jeff
ok if your hypothesis is correct , then how the hell , wordpress understand what page redirected to index.php while redirection happens in htaccess ?!
Mac Taylor
Like I said, all requests go to index.php.
Jeff
I think I see the confusion. It's not a redirect, it's a rewrite. The web server passes all the original request parameters to index.php, including the request path. Does that answer your question?
Jeff
@jeff im confused , if u look at at htaccess file of wordpress , you will notice , a simple redirection to index.php . when its redirected to index.php how it can guess what parameters there are .update your answer , if you can tell me in a more simple way
Mac Taylor
It is not a *redirect*. It's a *rewrite*. Those are different things. I'll update the answer with more detail.
Jeff
Dan
A: 

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.

TheDeadMedic