301 Redirect Using .htaccess

.htaccess 301 Redirection

If you attempt to turn RewriteEngine On a web server without the mod_rewrite module installed and enabled, this will cause a 500 Internal Server Error. Also, each time the request is made on a web server, .htaccess file will be called upon. 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.

Knowing this, let’s explore how to setup 301 Moved Permanently using Apache Server file called .htaccess

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> <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On #redirect ENTIRE old domain to NEW Domain (both www and non-www redirects) RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC] RewriteRule ^(.*)$ https://www.newdomain.net/$1 [L,R=301,NC] </IfModule> <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On #Force HTTPS SSL with htaccess RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] </IfModule> <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On #CHANGE file extension redirect RewriteCond %{REQUEST_URI} .php$ RewriteRule ^(.*).php$ /$1.html [R=301,NC,L] #you can also use this format to (perhaps helpful when redirecting to new domain) RewriteRule (.+)\.php?$ https://www.example.com/$1.html [R=301,NC,L] </IfModule> <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On #remove file extension redirect RewriteRule (.+)\.html?$ https://www.example.com/$1/ [R=301,NC,L] RewriteRule (.+)\.php?$ https://www.example.com/$1/ [R=301,NC,L] </IfModule> To force a directory to serve over SSL, create a .htaccess file in that folder and add. 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> What if you run in to issues 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.

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; }

By RankYa

RankYa digital marketer, website optimizer, content creator, and a fully qualified web developer helping businesses of all sizes achieve greater results online. Based in Melbourne Australia RankYa serves valued clients worldwide by providing personalized services.

We love sharing our proven experience through how to videos and complete courses related to business website marketing, conversion optimization, Google (Search Console, Ads, Analytics, YouTube), SEO, HTML5, Structured Data and WordPress WooCommerce Shopify. Thank you for visiting our blog.

1 comment

Questions? Leave a Comment!

Your email address will not be published. Required fields are marked *