301 Redirect Using .htaccess

Sometimes we need to change the URL of the page. Changing the URL is simple but there are few risks in this. If you are changing the URL of page which is popular and indexed in search engine, you are going to loose the visitors and ranking both. So, you should always do this process with extra care. Once you have changed the URL, redirect the old URL to the new to pass visitors and ranking both. This redirect should be 301 redirect. 301 redirect tells search engines that web page has been moved permanently to the new web address.
In this post, we will see how to do proper 301 redirection using htaccess file.
Where is .htaccess file on the server?
.htaccess file is located at the root folder of your website. You can easily find the program from any FTP program or file browser of cpanel. Be sure to show hidden files if you are unable to find this file on your website’s root directory.
Note: Take a backup of the file before any kind of changes.
Redirect single file
If you only want to redirect a single file, just add this line in the .htacess file.
RewriteEngine On Redirect 301 /oldfile.html http://www.yoursite.com/newFile.html
Redirect an entire site to new domain
This way of redirection also preserves old links. It means, www.olddomain.com/page.html will becone www.newsite.com/page.html
Redirect 301 / http://newsite.com/
Redirect all files with certain extension
RewriteEngine On RewriteCond %{REQUEST_URI} .php$ RewriteRule ^(.*).php$ /$1.htm [R=301,L]
Redirect from www to non-www location
RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} ^www.domain.com [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
Redirect to www location from non-www location
RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} ^domain.com [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
Similar Articles
1 Comments
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.
Hey Deepanker!!!! you write it well. I really don’t know about it. I thought it can only be done when we buy domain.