Wordpress can use the rewrite API to make the url more beautiful. Two related rewite functions are required: add_rewrite_tag and add_rewrite_rule. The following describes how to customize wordpress routing rules and url parameters.
Add_action ('init ', 'My _ rr_url ');
Function my_rr_url ()
{
// Ensure the $ wp_rewrite global is loaded
Global $ wp_rewrite;
Add_rewrite_tag ('% myname %', '([^ &] + )');
Add_rewrite_rule ('Haha/([A-Za-z0-9] {1 ,})/? $ ', 'Index. php? Page_id = 8 & myname = $ matches [1] ', 'top ');
// Call flush_rules () as a method of the $ wp_rewrite object
$ Wp_rewrite-> flush_rules ();
}
Add_rewrite_tag custom url parameters
You can use the add_rewrite_tag function to add custom url parameters. For example, & myname = in the preceding example tells wordpress that the myname parameter is valid. Therefore, use the add_rewrite_tag function to register this url parameter.
* The add_rewrite_tag function is not required to beautify wordpress URL routing rules. It is used only when you add custom parameters. For example, a parameter myname is added in this example.
Add_rewrite_rule custom route
To beautify the wordpress url, you must use the add_rewrite_rule function to customize url access rules and tell wordpress how to correctly parse the url. In the example above:
Add_rewrite_rule ('Haha/([A-Za-z0-9] {1 ,})/? $ ', 'Index. php? Page_id = 8 & myname = $ matches [1] ', 'top ');
The first matched parameter is $ matches [1], the second parameter is $ matches [2], and so on.
Familiar with the two functions: add_rewrite_tag and add_rewrite_rule, you can customize the routing rules of the wordpress website as you like.