问题描述
我是一个PHP开发人员,我目前正在开发一个CMS /博客系统。我想添加一个永久链接系统像WordPress。我很好奇WordPress解析永久链接。例如,如何获取 id
和的数据
:
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/
总之,我想要一个系统从URL中获取 id
和 post name
。我想让用户使用如下标签来改变WordPress的永久链接结构:
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%/
如何获取 $ id
和 $ post_name
其中值将 123
和示例
?请帮帮我。
提前感谢。
How do I get data in variables like $id
and $post_name
where values will 123
and example
? Please help me.Thanks in advance.
推荐答案
常用的apache模块 mod_rewrite 可以帮助你有了这个。你所做的是在.htaccess文件中写入重写规则,通过重写,通常类似于文件系统的奇特结构将作为$ _GET参数发送到PHP文件。
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.
例如,如果你想替换像?reactor = 13
到 / reactor / 13 /
For example, if you wanted to replace something like: ?reactor=13
into /reactor/13/
在.htaccess文件中写入此重写规则:
Write this rewrite rule in the .htaccess file:
RewriteEngine on
RewriteRule reactor/([0-9]+)/$ index.php?id=$1
现在,如果代替index.php你拉/ reactor / 13 /,你的index.php文件应该在$ _GET中看到:
Now, if instead of index.php you pull up /reactor/13/, your index.php file should see this in $_GET:
Array
(
[id] => 13
)
现在,对于你的HTML代码,由你来创建URL并服从你的思想结构。
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比插入这些编辑.htaccess文件更强大,它们将所有内容发送到WP,然后WP解决/路由其余的内部规则。
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.
这篇关于WordPress永久链接系统如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!