Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How WordPress permalink system works?

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.

like image 711
sani Avatar asked Jul 03 '12 15:07

sani


1 Answers

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.

like image 116
pp19dd Avatar answered Oct 01 '22 23:10

pp19dd