How to Redirect an Entire Site Using HTAccess

Configure 301 from htaccess

Businesswoman working on project on computer

Thomas Barwick / Getty Images

If you have a website you want to move to a new domain, one of the easiest ways to do it is with a 301 redirect in a .htaccess file in your web server root.

301 Redirects Are Important

It's important that you use a 301 redirect rather than a meta refresh or other type of redirect. This tells search engines that the pages have been permanently moved to a new location. Google and other search engines will then update their indexes to use the new domain without changing your indexing values. So, if your old website was ranking fairly well on Google, it will continue to rank well after the redirect is indexed. We have used 301 redirects for many of the pages on this site with no change in their rankings.​

  • Difficulty: Average
  • Time Required: 15 minutes

Here's How

  1. Place all your content on the new domain using the same directory structure and file names as the old domain. This is the most important step. In order for this 301 redirect to work, the domains need to be identical in file structure.

    • You might also consider putting in a noindex, nofollow robots.txt file on this new domain until you've got the redirect set up. This will ensure that Google and other search engines don't index the second domain and penalize you for duplicate content. But if you don't have a lot of content, or can get all the content copied over in a day or so, this isn't as important.
  2. On your old domain website, open up the

    .htaccess
    

    file in your root directory with a text editor — if you don't have a file called .htaccess (note the dot at the front), create one. This file may be hidden in your directory list.

  3. Add the line:

    redirect 301 / http://www.new domain.com/ 
    

    to the .htaccess file at the top.


  4. Change the URL

    http://www.new domain.com/
    

    to the new domain name you're redirecting to.

  5. Save the file to the root of your old website.

  6. Test that the old domain pages now point to the new domain.

    Depending on your server configuration, you may need to restart Apache for the change to take effect.

Format
mla apa chicago
Your Citation
Kyrnin, Jennifer. "How to Redirect an Entire Site Using HTAccess." ThoughtCo, Apr. 5, 2023, thoughtco.com/redirect-entire-site-using-htaccess-3467923. Kyrnin, Jennifer. (2023, April 5). How to Redirect an Entire Site Using HTAccess. Retrieved from https://www.thoughtco.com/redirect-entire-site-using-htaccess-3467923 Kyrnin, Jennifer. "How to Redirect an Entire Site Using HTAccess." ThoughtCo. https://www.thoughtco.com/redirect-entire-site-using-htaccess-3467923 (accessed April 19, 2024).