WordPress default .htaccess rules explained
WordPress default .htaccess rules explained - Photo by Jamie Street on Unsplash

WordPress default .htaccess rules explained

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.

WP default .htaccess rules

# 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>

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 ExplainedStart 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 ExplainedRewriteEngine On enables mod_rewrite

3. RewriteBase /

RewriteBase Directive

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

Rule ExplainedThis 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 ExplainedThis 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 ExplainedIf 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

Another Important read from Rohutech:

About the author

Rohan Kamble

A Web Developer by profession, who loves the web to the core with over 9 Years of Experience working on a wide range of domains like websites on e-commerce, Travel, Alumni, matrimonial, company's professional sites, Finance, company internal communications and many more. Also WordPress Plugins & Chrome extensions.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.