If you manage your own website (or your client’s) you will eventually require to explore redirection rules using .htaccess file.
Do keep in mind, most web server setups allow you to quickly and easily setup 301 redirections through web server “Redirect” options. If you have cPanel, then, you should first explore using cPanel Redirect instead of using .htaccess file.
Do keep in mind, always backup your current .htaccess file before modifying it, because redirection can cause issues with a live site. Complex redirections using regular expressions may require expert help (especially if using Search Console Site Address Change Tool).
As far as Search Engine Optimization and Performance is concerned. Each time the request is made to get content from a web server by a web site visitor, htaccess file will be rendered by the browser, always. That means, to optimize and better serve .htaccess file (particularly for Google’s Page Experience update), make sure you insert 301 redirection rules at precise points as they are needed.
WordPress example: you would want to place 301 redirection rules below the required WordPress codes in .htaccess file.
Knowing this, let’s explore how to setup 301 Moved Permanently using Apache Server file called .htaccess. These type of redirections will work on most web hosting platforms (for example: GoDaddy, HostGator, DreamHost, CrazyDomains, Bluehost, SiteGround just to name a few).
How to 301 Redirect .htaccess
Examples for .htaccess 301 Redirection
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^oldURL/ https://www.example.com/newURL/ [R=301,NC,L]
</IfModule>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#redirect folder to new folder (e.g. when you change category)
RewriteRule ^example-old-folder/(.*)$ https://www.example.com/new-folder/$1 [R=301,NC,L]
</IfModule>
Redirect Old Domain to New Domain via .htaccess (both www AND non-www redirects to WWW.newdomain.com)
<IfModule mod_rewrite.c>
RewriteEngine On
#redirect ENTIRE old domain to NEW Domain
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [L,R=301,NC]
</IfModule>
# 301 REDIRECT HTTP TO HTTPS AND WWW TO NON-WWW via .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
HTTPS Redirection over Server Port 80
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
Redirection to Change File Extension Using .htaccess
Redirect .php to / forwardslash or .html to .php or vice versa.
In this example, we changing php to html
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.html [R=301,NC,L]
RewriteRule (.+)\.php?$ https://www.example.com/$1.html [R=301,NC,L]
</IfModule>
In this example, we changing html to php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} .html$
RewriteRule ^(.*).html$ /$1.php [R=301,NC,L]
RewriteRule (.+)\.html?$ https://www.example.com/$1.php[R=301,NC,L]
</IfModule>
In this example, we’re removing .php file extension so that forward slash is used.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.+)\.php?$ https://www.example.com/$1/ [R=301,NC,L]
</IfModule>
In this example, we’re removing .html file extension so that forward slash is used.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.+)\.html?$ https://www.example.com/$1/ [R=301,NC,L]
</IfModule>
Force a Directory to Serve Over HTTPS
To force a directory to serve over SSL, create a .htaccess file inside that subfolder and add the below code. Ideal for HTTPS Failing issues on subdomains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} samplefolder
RewriteRule ^(.*)$ https://www.example.com/samplefolder/$1 [R,L]
</IfModule>
Redirect WordPress Site to New Domain Using .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
</IfModule>
Redirection of WordPress Images to New Domain
When you want to move your images from wp-content/uploads to a new site, simply add this to your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^wp\-content\/uploads\/?(.*)$ "http\:\/\/www\.yourdomain\.com\/$1" [R=301,L]
</IfModule>
What if you run in to issues with WordPress and .htaccess file RewriteRules do not work as expected? Then, change their location, for example: WordPress has default .htaccess rules for it to work correctly, depending on what you are redirecting, if you place the 301 rules above this default code, perhaps this may solve problems when 301 redirects aren’t working.
If worse comes comes to worse, simply revert back your backup of .htacess file.
301 Redirection on NGINX Server
Creating 301 redirection on NGINX server is bit different in a sense that redirection rules are added in nginx.conf file. Below is 301 redirect example for nginx:
server {
rewrite ^/oldURL/$ /newURL/ permanent;
}
I really appreciate this post a lot. Thanks!