Redirect an old domain to a new one with htaccess

If you're migrating to a new domain or, need to re-route a sub domain, you can achive this using an htaccess file. This article provides some useful code snippets.

This snippet is for a straight up redirect from or to The re-write rule will redirect with all url paths to the new domain:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC,OR]
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

To re-route specifc pages that are permenantly changing, you would use a 301 redirect like so:

Redirect 301 /an-old-url /the-new-url

To redirect the root of a domain, but specifically only the root you can use the following code (in this example we're wanting to forward to and individual redirects would be handled seperately (i.e. with a regex or 301 redirects as above):

RewriteCond %{HTTP_HOST} blog.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ [L,R=301]
Sign Up

NEXT: Set up a local server with wordpress

In this post I'll be running you through all the steps you need to get you set up with a local server environment running with a copy of WordPress so you can play about with the code without worrying about who's looking at it or if you break it.

comments powered by Disqus

Popular Tags

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.