我正在努力学习WordPress如何使用重写API重写漂亮到丑陋的URL。我有一些关于重写如何工作的想法。htaccess文件,而不是wp如何处理它。我正在阅读源代码和一本书。所以我的理解是。根目录中WP附带的htaccess文件只检查请求是否是WP登录之类的文件。php与否,如果不是,则将请求转发到索引。php。实际的重写规则存储在DBviz中。重写\\u规则(在wp\\u选项表中)。在源代码中,请参阅WP::parse\\u request call
$rewrite = $wp_rewrite->wp_rewrite_rules();
如果我执行var\\u dump($rewrite),我会看到一个数组,我假设它是重写规则
在WP\\u Rewite内::rewrite\\u rules()我明白了
return $this->rules;
因此,我假设$wp\\u rewrite->rules应该包含基于mypermalink设置的所有规则。如果在函数中输入var\\u dump($wp\\u rewrite)。php规则属性始终设置为NULL。var\\u转储如下所示:
WP_Rewrite (object) [Object ID #597][24 properties]
permalink_structure: (string) "/%postname%/"
use_trailing_slashes: (boolean) true
author_base: (string) "author"
search_base: (string) "search"
comments_base: (string) "comments"
pagination_base: (string) "page"
comments_pagination_base: (string) "comment-page"
feed_base: (string) "feed"
front: (string) "/"
root: (string) ""
index: (string) "index.php"
matches: (string) ""
rules: (null) NULL
我所关注的这本书显示了rules属性以包含规则
WP_Rewrite Object (
...
[permalink_structure] = > /%year%/%postname%/
[use_trailing_slashes] = > 1
...
[rules] = > Array (
[category/(.+?)/?$] = > index.php?category_name=$matches[1]
[tag/([^/]+)/page/?([0-9]{1,})/?$] = > index.php?tag=$matches[1] & paged=
$matches[2]
[tag/([^/]+)/?$] = > index.php?tag=$matches[1]
[(.+?)/trackback/?$] = > index.php?pagename=$matches[1] & tb=1
...
)
[endpoints] = > Array ()
...
)
我的问题是为什么$wp\\u rewrite->rules是空的?规则在代码中实际存储在哪里?
我的第二个问题是,我知道实际的URL重写是使用完成的。htaccess文件(如中所述https://wordpress.org/support/article/using-permalinks/)
When you create or update a “pretty” permalink structure, WordPress will generate rewrite rules and attempt to insert them into the proper .htaccess file.
但是。我在根目录中看到的htaccess文件似乎一直都有相同的规则。WP正在其他方面创建这些规则。htaccess文件(可能是不同的子文件夹)?