函数文档

got_mod_rewrite()

💡 云策文档标注

概述

got_mod_rewrite() 函数用于检测服务器是否运行 Apache 并加载了 mod_rewrite 模块。它返回布尔值,并可通过 'got_rewrite' 过滤器进行修改。

关键要点

  • 函数返回布尔值,指示 Apache 服务器是否启用了 mod_rewrite 模块。
  • 内部调用 apache_mod_loaded() 函数来检查模块状态。
  • 提供 'got_rewrite' 过滤器,允许开发者自定义返回结果,但注意此过滤器已不推荐用于强制 URL 重写,应改用 got_url_rewrite() 函数。
  • 函数自 WordPress 2.0.0 版本引入,相关钩子和函数包括 apply_filters() 和 got_url_rewrite()。

代码示例

function got_mod_rewrite() {
    $got_rewrite = apache_mod_loaded( 'mod_rewrite', true );
    return apply_filters( 'got_rewrite', $got_rewrite );
}

注意事项

此函数仅适用于 Apache 服务器;对于其他服务器(如 nginx),应使用 got_url_rewrite() 函数来检测 URL 重写支持。


📄 原文内容

Returns whether the server is running Apache with the mod_rewrite module loaded.

Return

bool Whether the server is running Apache with the mod_rewrite module loaded.

Source

function got_mod_rewrite() {
	$got_rewrite = apache_mod_loaded( 'mod_rewrite', true );

	/**
	 * Filters whether Apache and mod_rewrite are present.
	 *
	 * This filter was previously used to force URL rewriting for other servers,
	 * like nginx. Use the 'got_url_rewrite' filter in got_url_rewrite() instead.
	 *
	 * @since 2.5.0
	 *
	 * @see got_url_rewrite()
	 *
	 * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
	 */
	return apply_filters( 'got_rewrite', $got_rewrite );
}

Hooks

apply_filters( ‘got_rewrite’, bool $got_rewrite )

Filters whether Apache and mod_rewrite are present.

Changelog

Version Description
2.0.0 Introduced.