How-to Guide on 301 redirects and mod_rewrite to permanently move an old website to a new address without losing traffic.
Now don’t lose traffic also keep the Google bot happy as well. This guide will help you to ensure all the traffic to your old domain instantly redirect to the new domain. This helps you to keep trust in your brand rather than an ugly 404 error page or “Come Back Later” page” which nobody will come back again. Straight forward now go to the following codes into your .htaccess file. [Always remember, if you are not an expert in editing a .htaccess file, keep a working backup of your website before touching any code. If somehow your site breaks, the habit of backing up will save your time & hard work.] Let’s get started.
Method 1:
Options +FollowSymlinks
RewriteEngine On RewriteCond %{HTTP_HOST} ^olddomain.com [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301] RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
Method 2:
Options +FollowSymlinks
RewriteEngine On RewriteCond %{HTTP_HOST} \olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Method 3:
Options +FollowSymLinks
RewriteEngine on RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Method 4:
Redirect 301 / http://www.newdomain.com/
Note: Write or place the code before the line RewriteBase /
Otherwise, it will only redirect the main domain and not the posts.
Those were the methods. Now know how this simple code keeps the Search Engines bot happy.
Whenever a search bot come to crawl your website and finds that the domain returns a 404 error page or the website don’t load anymore, the bot informs the Search Engine like Google, Yahoo, Bing, Duckduckgo, etc. that the web address is no longer available means the website is dead. Thus the Search Engines promptly remove the webs pages from their index. If this happens, then the traffic coming to your old website go in vain. By the way, you can still redirect and regain reputation by forwarding the old site to your new website. If you redirect your old domain, then Google also updates its index as the change of address which helps you to preserve the old rankings. Not only that the search engines also know the all inbound links point to your old website passes the juice, PageRank and other attributes to the new one and thus preserving your reputation among the users.
How to Edit .htaccessfile on your server
If you don’t know how to edit the .htaccess file on your server where the advanced server settings are controlled.
If you use WordPress CMS, then install the “File Manager” plugin to access the files of your website on the server. If you don’t use WordPress, then connect to your server using your favourite FTP client. My Favorite FTP client is FileZilla. For shared hosting, you can directly access your Cpanel.
In the main root directory usually, it is public_html or www there is already a file exit called .htaccess. In shared hosting, you may not see the file even it exists, which may sure that your hidden files are visible or marked as shown from your Cpanel settings. In WordPress or Cpanel, you can directly edit the file from your Cpanel dashboard or plugin dashboard. But in FTP client FileZilla, you can’t make directly right select and edit them. Therefore you need to upload or update a new .htaccess file via the FTP client method in the case of hosting providers(VPS) where direct access or Cpanel is not given.
Note: I would strongly recommend making a backup of your .htaccess file, just in case things went wrong.
For WordPress and other CMS of static websites, append the code beneath the line RewriteEngine On.
To edit in FTP clients like CuteFTP, FileZilla download the already existing .htaccess file on your desktop and make a backup copy of it. Rename the original .htaccess file as htaccess.txt as windows can’t understand or may not open the .htaccess filename. If you don’t have already a .htaccess file, then don’t worry, just create a htaccess.txt file on your Desktop. Make changes to the text file and upload it then rename it as .htaccess again to make the effect.
For example, I have shown here the example on Method 2 to how your .htaccess file should look like-
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} \olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
Please keep the following in mind in regards to updating your .htaccess file
- Always be sure to upload .htaccess files in ASCII mode. Using binary in your FTP client will likely cause problems and make your server unhappy. Most FTP clients should do this automatically.
- If .htaccess does not work, there’s a good chance you’re on a Windows server and will need to use other methods!
- Many operating systems don’t allow you to create a file without something before the “.” — in this case, it would be best to save the file as htaccess.txt and change it once you’ve uploaded it to your server via your FTP client.
- Ensure your FTP client shows .htaccess files (FileZilla does and is free). Some FTP clients require you to turn this option on manually.
- Double-check that you’re not overwriting an old .htaccess file (many servers already place one there for any custom 404 pages and so forth.)
Leave a Reply