WordPress default htaccess rules explained – This article is a step by step guide to understand what exactly WordPress default htaccess file consists of and what does it mean.
These rules are mostly written by WordPress to your htaccess file at the time of installation or even when you try to update your permalink structure from /wp-admin/options-permalink.php this page.
Page Contents
WordPress default htaccess rules explained
# 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
Note: Just to clear out doubts for total beginners who have no clue of what are they getting into, this note is only for them.
#(hash) is used to comment a line in htaccess file, therefore in the above code you can say the very first line “BEGIN WordPress” is a comment. Similarly, at the end “END WordPress” is also a comment.
Let the Explanation of WordPress default htaccess rules Begin
1. <IfModule mod_rewrite.c>
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
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 /
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]
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
Hope this article “WordPress default htaccess rules explained” helps you understand the htaccess rules. Also help me to make this article even more stronger for others to understand well. Your feedback is very important for me, so kindly let me know in the comments what do you think about this article.