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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With