Understanding the Default WordPress .htaccess

 

The Default Rules

The default WordPress .htaccess rules are responsible for how WordPress is able to support ‘pretty permalinks’. Without these rules in place, WordPress permalinks would not resolve correctly. This feature allows your URLs to look much cleaner and more readable without over complicating or cluttering your website’s files structure.

The default rules look as follows:


# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

# END WordPress



LET THE EXPLANATION OF WORDPRESS DEFAULT HTACCESS RULES BEGIN

1. <IFMODULE MOD_REWRITE.C>

Apache Module mod_rewrite

Short Description – Provides a rule-based rewriting engine to rewrite requested URLs on the fly.

mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.

Rule Explained – Start of mod_rewrite module block, check if the ‘mod_rewrite’ module is enabled and if enabled only then everything inside the <IfModule mod_rewrite.c> and </IfModule> tags will execute.

2. REWRITEENGINE ON

RewriteEngine Directive

Short Description – Enables or disables runtime rewriting engine ( mod_rewrite )

Basically, when RewriteEngine directive is set to On only then module mod_rewrite will be able to modify or manipulate URLs otherwise not. All the subsequent RewriteRules will work only if the RewriteEngine directive is On.

Rule Explained – RewriteEngine On enables mod_rewrite

3. REWRITEBASE /

RewriteBase Directive

Short Description – Sets the base URL for per-directory rewrites

Rule Explained – This means whatever the rewrite rules follow after this directive applies to the root (/) of the website directory

4. REWRITERULE ^INDEX\.PHP$ – [L]

RewriteRule

Short Description – Defines rules for the rewriting engine
Syntax – RewriteRule Pattern Substitution [flags]

Rule Explained – This line says that whenever there is a request for index.php do nothing and stop the execution with no further rules will be processed.

5. REWRITECOND %{REQUEST_FILENAME} !-F REWRITECOND %{REQUEST_FILENAME} !-D REWRITERULE . /INDEX.PHP [L]

Short Description – REQUEST_FILENAME ( server-variable ) – The full local filesystem path to the file or script matching the request. In short, this server variable matches the requested directory or filename on the server.

-d – Is directory?
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.

-f – Is a regular file?
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.

Rule Explained – If a given request is not pointing to valid filename or directory, then redirect it to index.php and stop the execution with no further rules to be processed.

6. </IFMODULE>

End of mod_rewrite module block

ABOUT HTACCESS

The .htaccess is a distributed configuration file, and is how Apache handles configuration changes on a per-directory basis.

WordPress uses this file to manipulate how Apache serves files from its root directory, and subdirectories thereof. Most notably, WP modifies this file to be able to handle pretty permalinks.

For Multisite WordPress htaccess rules are different. For more information you can always check htaccess article on wordpress.org

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation