How To Redirect site with help of HTAccess by 301 Redirect

This article is about the Redirect the site with the help of HTAccess with 301 redirects. This document explains how to create a .htaccess file to redirect the content of your site. This will not redirect any email for your domains. A permanent redirect to your .htaccess file allows search engines and other users to know that an old link has been replaced by a new one. This is the recommended way to direct traffic from an existing page. Before you start How to redirect the site with HTAccess, please read me note below.


As a configuration file, .htaccess is very powerful. Even the slightest syntax error (such as a missing space) can prevent your content from displaying correctly or not displaying at all.

Since .htaccess is a hidden system file, make sure your FTP client is set up to show hidden files. This is usually an option in the program’s preferences/options.

Read also: How to optimize the website for Google

If you want to move a website to a new domain, one of the easiest and most effective ways is to perform a 301 redirect to a .htaccess file from the root of your web server.

301 redirects are important to Redirect the site with the help of HTAccess

It is important to use a 301 redirect instead of a meta-refresh or another type of redirect. This tells the search engines that the pages have been moved permanently to a new location. Google and other search engines will update their indexes to use the new domain without changing their indexing values. Therefore, if your previous website was ranked well on Google, it would continue to figure well after indexing the redirect. Personally, I used 301 redirects for most pages of this site without changing their classification.

  • Difficulty: Average
  • Required time: 15 minutes

How to redirect an entire site using HTAccess using 301 redirect

  1. Put all your content in the new domain using the same directory structure and file names as the previous domain. This is the most important step. For this redirection 301 to work, the domains must be identical in the file structure.
    Note: You can also consider placing a robots.txt file noindex, nofollow in this new domain until the redirection is configured. This will ensure that Google and other search engines do not index the second domain and do not penalize it for duplicate content. But if you have little content or you can copy all the content in a day or two, it’s not that important.
  2. On your old domain website, open the .htaccess file in your root directory with a text editor; If you do not have a file called .htaccess (look at the dot in front), create one. This file may be hidden in your directory list.
  3. Add the line:
    redirect 301 /
    to the .htaccess file at the top.
  4. Change the URL
    the new domain name to which you are redirecting.
  5. That’s all!
  6. Save the file to the root of your previous website.
  7. Verify that the old domain pages are now pointing to the new domain.
  8. Note: be sure to update your webmaster for example – Google Webmaster so as not to lose SEO.

Redirect the site with the help of HTAccess

Alternative method – How to Redirect the site with the help of HTAccess but exclude or more directories

Personal Experience – We recently wanted to move a website to a new domain and automatically send visitors and Googlebot to the new content, but some resources had to stay in the current domain. This meant that we had to redirect all requests to the site, with the exception of the content of a few directories, to a new domain. After some criticism, we decided that the simplest answer was a RewriteRule in our .htaccess file.

We used the following mod_rewrite rule:

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/test/
RewriteCond %{REQUEST_URI}!^/my-folder/
RewriteRule (.*)$1 [R=301,L]

This redirects (all the time with a 301 redirect) all site traffic to, with the exception of directory/test resource requests and / my file. We transfer the user to the exact resource requested with the help of the capture group (. *), Then including $ 1 in the new URL.

Conclusion – Redirect Site Using 301 Redirection in HTAccess

I hope you like the article, How to redirect an entire site with the help of HTAccess | If you have a problem with this article, send us an @compsmag tweet or contact us directly on FB / compsmag. You can also use our contact form

It’s all there: how to redirect a site using the HTAccess 301 redirect, which is the permanent way to move the old site to a new site without losing your PageRank. If we miss something then contact us please to fix it.

Thank you for reading the article 🙂

Recommended from our sister site – Best CMS Platforms (Content Management System) – To create a big or small website or blog…

Hope you like the article How To Redirect site with help of HTAccess by 301 Redirect. If you still have doubt ask via comment or directly contact us.


Trickbugs - Get the latest Trick and Tips