Helpful .htaccess Rewrite Rules for WordPress

The .htaccess is a configuration file and is used to alter the configuration of the Apache Web Server. We can enable/disable the additional functionality through this configuration file.

Today, I am sharing some common Rewrite Rules that most of the peoples need to alter some Web Server configuration. WordPress comes with pre-build rewrite rules in the .htaccess file.

If in case, you don’t find the .htaccess file from the root directory then you can generate it through Dashboard > Settings > Permalinks.

This is the default .htaccess file from the WordPress directory.

Please make sure to take a backup before doing any changes.

# 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]
# END WordPress

Table of content

What is 301 Permanent Redirect

The 301 is an HTTP Status Code, that tells the search engine about the URL is permanently moved to another URL. Not just for URL only, you can use it with pages, domains, sub-domains, etc.

Remove .html From URL

In some cases, peoples required to remove the .html extension from the URL. It’s pretty easy to handle from the Dashboard > Settings > Permalinks.

Permalinks Settings

But as per the SEO point of view, we need to tell the search engine that our old URL is permanently moved to the new one. Thanks to .htaccess file, We can permanently redirect all the posts or pages that have .html extension to non .html routes.

RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]

Remove Year/Month/Date From URL

Similar to the previous point, if you required to remove the Year/Month/Date or Year/Month from the URL then you can achieve it by following the Permalinks settings.

But changing in permalink is not enough. You have to update that to all the search engine too so that the traffic on your site don’t distract. Below is the example of how you remove the Year/Month/Date from the URL but single line of code.

RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ /$4

Single Page Redirection

You don’t need the regular expressions in case of a single post redirection. Just use the old and the new URL. Let’s see it through the example.

Redirect 301 /old-page.html

Redirect www to non-www

We should keep our site to work with WWW or with our WWW. By keeping both will confuse the search engine and the visitors too. Whether you keep WWW or non-www, everything is up to you. But keeping one is good. Let’s say we keep our domain without www ( and if someone hits the domain with www (, then it will display the server error.

We have to add the following 2 lines that will check if someone uses the www in the website name, then we will redirect the visitor to the non-www site.

RewriteCond %{HTTP_HOST} ^www\.domain-name\.com [NC]
RewriteRule ^(.*)$$1 [L,R=301]

But if you need to redirect the non-www visitor to the www version of the site, then you have to use the below codes.

Redirect to WWW

RewriteCond %{HTTP_HOST} ^domain-name\.com [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Redirect HTTP to HTTPS

HTTPS is the secure and recommended protocol by all the search engines. And you have to add the SSL Certificate for your domain. CPanel has the Free SSL Certificate, which you can apply very easily on the particular domain. By adding the SSL Certificate is not enough, you have to redirect all the HTTP requests to HTTPS.

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

I hope you guys enjoyed this article and if you like this article, then please follow us for more interested and helpful tutorials. You can follow us on Facebook and Twitter.