WP_Rewrite::mod_rewrite_rules(): string
- Since
- 1.5.0
- Source
wp-includes/class-wp-rewrite.php:1539
Description
Does not actually write to the .htaccess file, but creates the rules for the process that will.
Will add the non_wp_rules property rules to the .htaccess file before the WordPress rewrite rules one.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
string
Performance profile
How much work a call to WP_Rewrite::mod_rewrite_rules() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 4–75
- Plugin surface
- 2 hooks
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 116.
Third-party callbacks on 'mod_rewrite_rules', 'rewrite_rules' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Rewrite::mod_rewrite_rules() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->using_permalinks() | 4 | ->using_permalinks() |
->using_permalinks() && !isset($site_root) && !isset($home_root) | 53–54 | ->using_permalinks(), site_url(), parse_url(), home_url(), parse_url(), apply_filters() |
->using_permalinks() && !isset($site_root) && isset($home_root) | 59–60 | ->using_permalinks(), site_url(), parse_url(), home_url(), parse_url(), trailingslashit(), apply_filters() |
->using_permalinks() && isset($site_root) && !isset($home_root) | 59–60 | ->using_permalinks(), site_url(), parse_url(), trailingslashit(), home_url(), parse_url(), apply_filters() |
->using_permalinks() && !isset($site_root) && !isset($home_root) | 61–63 | ->using_permalinks(), site_url(), parse_url(), home_url(), parse_url(), ->rewrite_rules(), apply_filters() |
->using_permalinks() && isset($site_root) && isset($home_root) | 65–66 | ->using_permalinks(), site_url(), parse_url(), trailingslashit(), home_url(), parse_url(), trailingslashit(), apply_filters() |
->using_permalinks() && !isset($site_root) && isset($home_root) | 67–69 | ->using_permalinks(), site_url(), parse_url(), home_url(), parse_url(), trailingslashit(), ->rewrite_rules(), apply_filters() |
->using_permalinks() && isset($site_root) && !isset($home_root) | 67–69 | ->using_permalinks(), site_url(), parse_url(), trailingslashit(), home_url(), parse_url(), ->rewrite_rules(), apply_filters() |
->using_permalinks() && isset($site_root) && isset($home_root) | 73–75 | ->using_permalinks(), site_url(), parse_url(), trailingslashit(), home_url(), parse_url(), trailingslashit(), ->rewrite_rules(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 116 | 4–75 | 9 | |
| 8.5 | 116 | 4–75 | 9 | |
| 8.4 | 116 | 4–75 | 9 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 125 | 4–75 | 9 | |
| 8.2 | 125 | 4–75 | 9 | |
| 8.1 | 125 | 4–75 | 9 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 126 | 4–75 | 9 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while WP_Rewrite::mod_rewrite_rules() runs, in this order:
- apply_filters( mod_rewrite_rules )filterline 1605 (+66 into the body)
Filters the list of rewrite rules formatted for output to an .htaccess file.
- do_action( rewrite_rules )filter_deprecatedline 1615 (+76 into the body)
Filters the list of rewrite rules formatted for output to an .htaccess file.
Uses · 8
- site_url()Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
- trailingslashit()Appends a trailing slash.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
- WP_Rewrite::using_permalinks()Determines whether permalinks are being used.
- WP_Rewrite::rewrite_rules()Constructs rewrite matches and queries from permalink structure.
Source code
public function mod_rewrite_rules() { if ( ! $this->using_permalinks() ) { return ''; } $site_root = parse_url( site_url() ); if ( isset( $site_root['path'] ) ) { $site_root = trailingslashit( $site_root['path'] ); } $home_root = parse_url( home_url() ); if ( isset( $home_root['path'] ) ) { $home_root = trailingslashit( $home_root['path'] ); } else { $home_root = '/'; } $rules = "<IfModule mod_rewrite.c>\n"; $rules .= "RewriteEngine On\n"; $rules .= "RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n"; $rules .= "RewriteBase $home_root\n"; // Prevent -f checks on index.php. $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all). foreach ( (array) $this->non_wp_rules as $match => $query ) { // Apache 1.3 does not support the reluctant (non-greedy) modifier. $match = str_replace( '.+?', '.+', $match ); $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; } if ( $this->use_verbose_rules ) { $this->matches = ''; $rewrite = $this->rewrite_rules(); $num_rules = count( $rewrite ); $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . "RewriteCond %{REQUEST_FILENAME} -d\n" . "RewriteRule ^.*$ - [S=$num_rules]\n"; foreach ( (array) $rewrite as $match => $query ) { // Apache 1.3 does not support the reluctant (non-greedy) modifier. $match = str_replace( '.+?', '.+', $match ); if ( str_contains( $query, $this->index ) ) { $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; } else { $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; } } } else { $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . "RewriteCond %{REQUEST_FILENAME} !-d\n" . "RewriteRule . {$home_root}{$this->index} [L]\n"; } $rules .= "</IfModule>\n"; /** * Filters the list of rewrite rules formatted for output to an .htaccess file. * * @since 1.5.0 * * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. */ $rules = apply_filters( 'mod_rewrite_rules', $rules ); /** * Filters the list of rewrite rules formatted for output to an .htaccess file. * * @since 1.5.0 * @deprecated 1.5.0 Use the {@see 'mod_rewrite_rules'} filter instead. * * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. */ return apply_filters_deprecated( 'rewrite_rules', array( $rules ), '1.5.0', 'mod_rewrite_rules' ); }Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-rewrite.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.