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. And 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.

# 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

Table of content

What is 301 Permanent Redirect

If you are not familiar with 301 permanent redirects, then don’t worry. The 301 is an HTTP Status Code, which tells the search engine that 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 for SEO point, 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

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 (https://domain-name.com) and if someone hits the domain with www (https://www.domain-name.com), 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 ^(.*)$ https://domain-name.com/$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 ^(.*)$ https://www.domain-name.com/$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]

Please let us know through comments if you don’t find what you are looking for or hows it helps you.

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.