How to Redirect Using htaccess (301)

Need to redirect one site to another? Perhaps you’re moving your WordPress website to a new location.

Looking to use .htaccess to improve your site’s SEO? Maybe you need to redirect one URL to another within your WordPress website.

Take a look at this quick and easy guide to the basics of 301 redirects using .htaccess.

.htaccess 301 Redirect (Domain 1 -> Domain 2)

[adsense_id=”1″] When moving a site from one domain to another, you typically want the same directory and file structure reflected in your redirect, and the code below will do this for you. This will allow domain1.com to forward to domain2.com under all circumstances; for example, a call for http://www.domain1.com/this-is-a-post/ will redirect to http://www.domain2.com/this-is-a-post/

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

Simply place the .htaccess file at the public root of domain1.com, replacing domain2.com in the code with your new domain. Not only will this redirect traffic, but it will also alert search engines like Google that your web address has permanently changed, encouraging them to deindex the old site and freshly index the new.

.htaccess 301 Redirect (domain.com <-> www.domain.com)

To keep all of the link love your site receives in one place, many webmasters like to redirect domain1.com to www.domain1.com, or vice versa. The code below will do this for you.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Place this .htaccess file at the public root of your site, replacing domain.com (two instances) in the code with your actual domain name. This will push all of the so-called link juice to a single domain, whether your fans and readers link to your URL with a “www” or not.

Share Your Thoughts

*