* The htaccess rewrite rule operation class.
* Responsible for reading, writing, and generating .htaccess rules used by LiteSpeed Cache.
defined( 'WPINC' ) || exit();
* Provides utilities to locate, read, backup, and write .htaccess files for both frontend and backend,
* as well as generate and manage LiteSpeed-specific rule blocks.
class Htaccess extends Root {
* Absolute path to the frontend `.htaccess`.
private $frontend_htaccess = null;
* Default/auto-detected frontend `.htaccess` path before filters/overrides.
private $_default_frontend_htaccess = null;
* Absolute path to the backend `.htaccess`.
private $backend_htaccess = null;
* Default/auto-detected backend `.htaccess` path before filters/overrides.
private $_default_backend_htaccess = null;
* Whether the frontend `.htaccess` (or its directory) is readable.
private $frontend_htaccess_readable = false;
* Whether the frontend `.htaccess` (or its directory) is writable.
private $frontend_htaccess_writable = false;
* Whether the backend `.htaccess` (or its directory) is readable.
private $backend_htaccess_readable = false;
* Whether the backend `.htaccess` (or its directory) is writable.
private $backend_htaccess_writable = false;
* Lines that turn on and guard rewrite/module blocks.
* Lines that turn on and guard general rewrite/module blocks.
private $__rewrite_general;
const LS_MODULE_START = '<IfModule LiteSpeed>';
const EXPIRES_MODULE_START = '<IfModule mod_expires.c>';
const LS_MODULE_END = '</IfModule>';
const LS_MODULE_REWRITE_START = '<IfModule mod_rewrite.c>';
const REWRITE_ON = 'RewriteEngine on';
const LS_MODULE_DONOTEDIT = '## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##';
const MARKER = 'LSCACHE';
const MARKER_NONLS = 'NON_LSCACHE';
const MARKER_LOGIN_COOKIE = '### marker LOGIN COOKIE';
const MARKER_ASYNC = '### marker ASYNC';
const MARKER_CRAWLER = '### marker CRAWLER';
const MARKER_MOBILE = '### marker MOBILE';
const MARKER_NOCACHE_COOKIES = '### marker NOCACHE COOKIES';
const MARKER_NOCACHE_USER_AGENTS = '### marker NOCACHE USER AGENTS';
const MARKER_CACHE_RESOURCE = '### marker CACHE RESOURCE';
const MARKER_BROWSER_CACHE = '### marker BROWSER CACHE';
const MARKER_MINIFY = '### marker MINIFY';
const MARKER_CORS = '### marker CORS';
const MARKER_WEBP = '### marker WEBP';
const MARKER_DROPQS = '### marker DROPQS';
const MARKER_START = ' start ###';
const MARKER_END = ' end ###';
* Initialize the class and set its properties.
public function __construct() {
$this->_default_frontend_htaccess = $this->frontend_htaccess;
$this->_default_backend_htaccess = $this->backend_htaccess;
$frontend_htaccess = defined( 'LITESPEED_CFG_HTACCESS' ) ? constant( 'LITESPEED_CFG_HTACCESS' ) : false;
if ( $frontend_htaccess && substr( $frontend_htaccess, -10 ) === '/.htaccess' ) {
$this->frontend_htaccess = $frontend_htaccess;
$backend_htaccess = defined( 'LITESPEED_CFG_HTACCESS_BACKEND' ) ? constant( 'LITESPEED_CFG_HTACCESS_BACKEND' ) : false;
if ( $backend_htaccess && substr( $backend_htaccess, -10 ) === '/.htaccess' ) {
$this->backend_htaccess = $backend_htaccess;
// Filter for frontend & backend htaccess path.
$this->frontend_htaccess = apply_filters( 'litespeed_frontend_htaccess', $this->frontend_htaccess );
$this->backend_htaccess = apply_filters( 'litespeed_backend_htaccess', $this->backend_htaccess );
// Frontend .htaccess privilege.
$test_permissions = file_exists( $this->frontend_htaccess ) ? $this->frontend_htaccess : dirname( $this->frontend_htaccess );
if ( is_readable( $test_permissions ) ) {
$this->frontend_htaccess_readable = true;
if ( is_writable( $test_permissions ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- Checking permissions, not file operations.
$this->frontend_htaccess_writable = true;
// General Rewrite Rules (Files/Logs protection)
$this->__rewrite_general = [
self::LS_MODULE_REWRITE_START, // <IfModule mod_rewrite.c>
self::REWRITE_ON, // RewriteEngine on
'RewriteRule ' . preg_quote(LITESPEED_DATA_FOLDER) . '/debug/.*\.log$ - [F,L]', // phpcs:ignore WordPress.PHP.PregQuoteDelimiter.Missing
'RewriteRule ' . preg_quote(self::CONF_FILE) . ' - [F,L]', // phpcs:ignore WordPress.PHP.PregQuoteDelimiter.Missing
self::LS_MODULE_END, // </IfModule>
'RewriteRule .* - [E=Cache-Control:no-autoflush]',
// Backend .htaccess privilege.
if ( $this->frontend_htaccess === $this->backend_htaccess ) {
$this->backend_htaccess_readable = $this->frontend_htaccess_readable;
$this->backend_htaccess_writable = $this->frontend_htaccess_writable;
$test_permissions = file_exists( $this->backend_htaccess ) ? $this->backend_htaccess : dirname( $this->backend_htaccess );
if ( is_readable( $test_permissions ) ) {
$this->backend_htaccess_readable = true;
if ( is_writable( $test_permissions ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- Checking permissions, not file operations.
$this->backend_htaccess_writable = true;
* Get if htaccess file is readable.
* @param string $kind 'frontend' or 'backend'.
private function _readable( $kind = 'frontend' ) {
if ( 'frontend' === $kind ) {
return $this->frontend_htaccess_readable;
if ( 'backend' === $kind ) {
return $this->backend_htaccess_readable;
* Get if htaccess file is writable.
* @param string $kind 'frontend' or 'backend'.
public function writable( $kind = 'frontend' ) {
if ( 'frontend' === $kind ) {
return $this->frontend_htaccess_writable;
if ( 'backend' === $kind ) {
return $this->backend_htaccess_writable;
* Get frontend htaccess path.
* @param bool $show_default Whether to return the default/auto-detected path.
public static function get_frontend_htaccess( $show_default = false ) {
return self::cls()->_default_frontend_htaccess;
return self::cls()->frontend_htaccess;
* Get backend htaccess path.
* @param bool $show_default Whether to return the default/auto-detected path.
public static function get_backend_htaccess( $show_default = false ) {
return self::cls()->_default_backend_htaccess;
return self::cls()->backend_htaccess;
* Check to see if .htaccess exists starting at $start_path and going up directories until it hits DOCUMENT_ROOT.
* As dirname() strips the ending '/', paths passed in must exclude the final '/'.
* @param string $start_path Absolute path to begin searching from (without trailing slash).
* @return string|false The directory containing .htaccess, or false if not found.
private function _htaccess_search( $start_path ) {
while ( ! file_exists( $start_path . '/.htaccess' ) ) {
if ( '/' === $start_path || ! $start_path ) {
$doc_root = ! empty( $_SERVER['DOCUMENT_ROOT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) : '';
if ( $doc_root && wp_normalize_path( $start_path ) === wp_normalize_path( $doc_root ) ) {
if ( dirname( $start_path ) === $start_path ) {
$start_path = dirname( $start_path );
* Set the path class variables.
private function _path_set() {
$frontend = Router::frontend_path();
$frontend_htaccess_search = $this->_htaccess_search( $frontend ); // The existing .htaccess path to be used for frontend .htaccess.
$this->frontend_htaccess = $frontend;
if ( $frontend_htaccess_search ) {
$this->frontend_htaccess = $frontend_htaccess_search;
$this->frontend_htaccess .= '/.htaccess';
$backend = realpath( ABSPATH ); // /home/user/public_html/backend/
if ( $frontend === $backend ) {
$this->backend_htaccess = $this->frontend_htaccess;
// Backend is a different path.
$backend_htaccess_search = $this->_htaccess_search( $backend );
// Found affected .htaccess.
if ( $backend_htaccess_search ) {
$this->backend_htaccess = $backend_htaccess_search . '/.htaccess';
// Frontend path is the parent of backend path.
if ( 0 === stripos( (string) $backend, $frontend . '/' ) ) {
// Backend uses frontend htaccess.
$this->backend_htaccess = $this->frontend_htaccess;
$this->backend_htaccess = $backend . '/.htaccess';
* Get corresponding htaccess path.
* @param string $kind Frontend or backend.
public function htaccess_path( $kind = 'frontend' ) {
$path = $this->backend_htaccess;
$path = $this->frontend_htaccess;
* Get the content of the rules file.
* NOTE: will throw error if failed.
* @since 2.9 Used exception for failed reading.
* @param string $kind 'frontend' or 'backend'.
* @return string The file content.
* @throws \Exception If the file is not readable or cannot be retrieved.
public function htaccess_read( $kind = 'frontend' ) {
$path = $this->htaccess_path( $kind );
if ( ! $path || ! file_exists( $path ) ) {
if ( ! $this->_readable( $kind ) ) {
$content = File::read( $path );
if ( false === $content ) {
$content = str_ireplace( "\x0D", '', $content );
* Try to backup the .htaccess file if we didn't save one before.
* NOTE: will throw error if failed.
* @param string $kind 'frontend' or 'backend'.
* @throws \Exception If backup fails.
private function _htaccess_backup( $kind = 'frontend' ) {
$path = $this->htaccess_path( $kind );
if ( ! file_exists( $path ) ) {
if ( file_exists( $path . '.bk' ) ) {
$res = copy( $path, $path . '.bk' );
// Failed to backup, abort.
* Get mobile view rule from htaccess file.
* NOTE: will throw error if failed.
* @return string The user agent regex for mobile detection.
* @throws \Exception If the rule cannot be found.
public function current_mobile_agents() {
$rules = $this->_get_rule_by( self::MARKER_MOBILE );
if ( ! isset( $rules[0] ) ) {
Error::t( 'HTA_DNF', self::MARKER_MOBILE );
$rule = trim( $rules[0] );
$match = substr( $rule, strlen( 'RewriteCond %{HTTP_USER_AGENT} ' ), -strlen( ' [NC]' ) );
Error::t( 'HTA_DNF', __( 'Mobile Agent Rules', 'litespeed-cache' ) );
* Parse rewrites rule from the .htaccess file.
* NOTE: will throw error if failed.
* @param string $kind 'frontend' or 'backend'.
* @return string The parsed login-cookie vary rule.
* @throws \Exception If the rule cannot be found or is invalid.
public function current_login_cookie( $kind = 'frontend' ) {
$rule = $this->_get_rule_by( self::MARKER_LOGIN_COOKIE, $kind );
Error::t( 'HTA_DNF', self::MARKER_LOGIN_COOKIE );
if ( 0 !== strpos( $rule, 'RewriteRule .? - [E=' ) ) {
Error::t( 'HTA_LOGIN_COOKIE_INVALID' );
$rule_cookie = substr( $rule, strlen( 'RewriteRule .? - [E=' ), -1 );
if ( LITESPEED_SERVER_TYPE === 'LITESPEED_SERVER_OLS' ) {
$rule_cookie = trim( $rule_cookie, '"' );
$rule_cookie = substr( $rule_cookie, strlen( 'Cache-Vary:' ) );
* Get rewrite rules based on the marker.
* @param string $cond Marker constant (e.g. self::MARKER_MOBILE).
* @param string $kind 'frontend' or 'backend'.
* @return string|array<int,string>|false Rule(s) or false if not found.
private function _get_rule_by( $cond, $kind = 'frontend' ) {
$path = $this->htaccess_path( $kind );
if ( ! $this->_readable( $kind ) ) {
$rules = File::extract_from_markers( $path, self::MARKER );
if ( ! in_array( $cond . self::MARKER_START, $rules, true ) || ! in_array( $cond . self::MARKER_END, $rules, true ) ) {
$key_start = array_search( $cond . self::MARKER_START, $rules, true );
$key_end = array_search( $cond . self::MARKER_END, $rules, true );
if ( false === $key_start || false === $key_end ) {
$results = array_slice( $rules, $key_start + 1, $key_end - $key_start - 1 );
if ( count( $results ) === 1 ) {
return trim( $results[0] );