save_mod_rewrite_rules()
云策文档标注
概述
save_mod_rewrite_rules() 函数用于更新 .htaccess 文件中的重写规则,仅在文件可写时执行。在单站点环境中,它返回写入操作的成功状态;在多站点环境中,则返回 null。
关键要点
- 函数检查 .htaccess 文件是否存在和可写,或目录可写且使用 mod_rewrite 固定链接。
- 如果服务器支持 mod_rewrite,则通过 insert_with_markers() 将规则写入 .htaccess 文件。
- 返回值:单站点中成功返回 true,失败返回 false;多站点中返回 null。
- 函数依赖于全局变量 $wp_rewrite 和相关辅助函数,如 get_home_path() 和 got_mod_rewrite()。
代码示例
function save_mod_rewrite_rules() {
global $wp_rewrite;
if ( is_multisite() ) {
return null;
}
// Ensure get_home_path() is declared.
require_once ABSPATH . 'wp-admin/includes/file.php';
$home_path = get_home_path();
$htaccess_file = $home_path . '.htaccess';
/*
* If the file doesn't already exist check for write access to the directory
* and whether we have some rules. Else check for write access to the file.
*/
if ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
|| is_writable( $htaccess_file )
) {
if ( got_mod_rewrite() ) {
$rules = explode( "n", $wp_rewrite->mod_rewrite_rules() );
return insert_with_markers( $htaccess_file, 'WordPress', $rules );
}
}
return false;
}
原文内容
Updates the htaccess file with the current rules if it is writable.
Description
Always writes to the file if it exists and is writable to ensure that we blank out old rules.
Source
function save_mod_rewrite_rules() {
global $wp_rewrite;
if ( is_multisite() ) {
return null;
}
// Ensure get_home_path() is declared.
require_once ABSPATH . 'wp-admin/includes/file.php';
$home_path = get_home_path();
$htaccess_file = $home_path . '.htaccess';
/*
* If the file doesn't already exist check for write access to the directory
* and whether we have some rules. Else check for write access to the file.
*/
if ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
|| is_writable( $htaccess_file )
) {
if ( got_mod_rewrite() ) {
$rules = explode( "n", $wp_rewrite->mod_rewrite_rules() );
return insert_with_markers( $htaccess_file, 'WordPress', $rules );
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |