Helpful .htaccess Rewrite Rules for WordPress

In the dynamic world of WordPress, mastering the .htaccess file can significantly enhance your website’s functionality and performance. While it may seem intimidating at first, understanding how to leverage rewrite rules can empower you to optimize URLs, improve SEO, and enhance user experience. In this guide, we’ll delve into some helpful rewrite rules tailored specifically for WordPress users, equipping you with the knowledge to take full control of your website’s routing.

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

Understanding Rewrite Rules:

Before we delve into specific rewrite rules, let’s grasp the concept behind them. Rewrite rules, defined in the .htaccess file, instruct the server on how to handle incoming requests. They enable you to transform URLs, redirect traffic, and customize permalinks without altering the underlying structure of your website. This flexibility is invaluable for WordPress users seeking to refine their site’s architecture and navigation.

Have a look at 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]
</IfModule>
# 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 https://www.yoursite.com/new-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 (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]

Conclusion:

Incorporating these helpful rewrite rules into your WordPress website can yield significant benefits in terms of SEO, user experience, and site management. By mastering the .htaccess file, you gain the ability to customize URL structures, implement redirects, and optimize routing with ease. Whether you’re a seasoned WordPress user or a novice, understanding these essential techniques is crucial for maximizing your website’s potential.

Remember, while .htaccess can be a powerful tool, it’s essential to exercise caution when making changes, as incorrect configurations can lead to unintended consequences. If you’re unsure or require assistance, don’t hesitate to seek help from a qualified WordPress developer. Hire WordPress developer today and unlock the full potential of your website!

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.