0
(0)

Last Updated on April 22, 2021 by Amit

How to modify WordPress htaccess file

In this article I will give you a quick overview of what the wordpress htaccess is , how it works and how you can manually add your custom code to your htaccess on WordPress site.
With WordPress blog CMS you get a default htaccess file that the WordPress uses to manipulate your blog URLs.

Your htaccess file on WordPress is responsible for pretty URLs of your site or blog.

Your WordPress/. htaccess looks something like :

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

You can see that the WordPress uses a simple set of Rules to redirect your blog URLs to the main index.php file.

WordPress forwards all requested URLs (not pointing to an existent file or directory) to the main index file . And the index file contains a script that checks the $_SERVER[‘REQUEST_URI’] variable to find data in databases.

Here is an example to demonstrate the wordpress URL rewriting logic :
To open a wordpress post, You type the post URL

https://example.com/22/10/hello-world

into your browser address bar.
The fist thing that happens on the server is , the WordPress htaccess is read.
Your WordPress htaccess then forwards the URL path to /index.php for processing.

The index.php file on WordPress root directory then reads the REQUEST_URI string from URL and returns the data from database.
It’s pretty simple. The URL Rewriting logic used by WordPress is awesome and easily understandable as it uses the mod-rewrite (a URL Rewriting module) provided by Apache server.

The htaccess file in your wordpress root directory doesn’t affect other directories and files on your server. The htaccess only forwards those paths that do not exist as an actual file or directory on your server.

The following two conditions used by WordPress htaccess prevent your real files and folders from being forwarded to the index.php

  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteCond %{REQUEST_FILENAME} !-d

The condition is there to protect your original files.
The second one protects your directories.

Quite simple.. isn’t it?

How to modify WordPress htaccess

Ah, this is a frequently asked question on the internet but here I will give you an unique solution and explain how you can add your custom code to htaccess.
Most often you will you need to edit your wordpress htaccess to add your custom Redirect or RewriteRule .
Some wordpress users who don’t know how to edit htaccess sometimes alter the wordpress default code and that results either in 500 server error or the htaccess code not being read by server at all.

Where to add the custom code?
This is really important to know where you should put your custom code in htaccess. Bellow WP rules or At the top because it really matters.
If you put your custom “RewriteRule” bellow the “WordPress rules” then your rule will never be found by the server as WP rule overrides it.

So you must keep your own RewriteRule at the top or before WordPress code .

Another important thing to note here is that if you are going to add a custom redirect using Redirect or RedirectMatch directive then you will need to replace those Redirects with RewriteRule otherwise your redirection won’t work.
RedirectMatch and Redirect are directives of Apache alias module while the RewriteRule is part of Mod-rewrite a different module . WordPress uses mod-rewrite so your custom redirects should also be using the same module. Simply change your Redirect or RedirectMatch to RewriteRule otherwise it will be overridden by the default WordPress rules.


If you are new to htaccess and want to learn some basics of URL Rewriting, you can follow my Htaccess tutorial for beginners .

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.