I am a php developer and i am currently developing a CMS/blog system. i want to add a permalink system like WordPress. i am curious about how WordPress parse permalink. For example, how to get data like id
and post name
from:
example.com/id/123/post/example/
In short, I want a system to get id
and post name
from the url. I want to enable users to change the permalink structure like WordPress using tags like:
id/%postid%/post/%postname%/
How do I get data in variables like $id
and $post_name
where values will 123
and example
? Please help me.
Thanks in advance.
The commonly available apache module mod_rewrite can help you out with this. What you do is write rewrite rules inside an .htaccess file, and through the rewrite, fancy structures that would have normally resembled a file system get sent to a PHP file as $_GET parameters.
For example, if you wanted to replace something like: ?reactor=13
into /reactor/13/
Write this rewrite rule in the .htaccess file:
RewriteEngine on
RewriteRule reactor/([0-9]+)/$ index.php?id=$1
Now, if instead of index.php you pull up /reactor/13/, your index.php file should see this in $_GET:
Array
(
[id] => 13
)
Now, for your HTML code, it's up to you to craft URLs and obey your thought-out structure.
These rewrite rules can be confusing, but they follow a logical regex pattern.
WordPress takes a stronger approach than inserting these editing .htaccess files, to where they send everything to WP, and then WP solves / routes the rest through internal rules.
This depends on what server software you're using, but if your planning on using your scripts on Apache it, usually, involves using something called mod_rewrite
and specifically a .htaccess
file.
I would recommend starting by reading a tutorial on this subject. As usual, Google is your friend.
来源:https://stackoverflow.com/questions/11314358/how-wordpress-permalink-system-works