A 301 is the HTTP response code that tells your browser that the site it visited has been permanently moved to somewhere else.
When moving a site, you can use 301s to automatically forward vistors from your old URLs to your new ones.
The simplest form, is using Apache’s
.htaccess files which allow you to control the
web server’s behaviour.
In the root of the location you want to redirect, add a file
.htaccess and put into it:
RedirectPermanent / http://my.new.domain/
Two things to note here:
First of all, the redirect is permanent. That means that if you make a mistake and want to go back, you have to clear your browser history. If it’s been out in the wild and your customers have accessed that redirect, you are stuffed because you cannot force your customers to clear their browsing history to fix your 301.
Secondly, this redirects the whole site. That is, any paths will be included in the redirect. So,
http://my.old.site/path1 will be redirected to
This may not be what you want. For example, when I was doing this recently, I wanted to move
an app from a path on a server to a subdomain of its own; i.e.
This first redirect does not work for this case.
To achieve that use case, you are best using a matching redirect but then ignoring the matching part:
RedirectMatch 301 ^/ http://path1.new.domain/
What this does is match the path, but then ignore it in the redirect. If I put this
.htaccess file into the root of
path1 folder it will redirect to the appropriate subdomain.
To get around the browser caching issue, some people suggest slightly more hacky use of the 302. HTTP 302 means the content has been temporarily moved and has been found elsewhere. If a URL is supplied in the header, the browser should go to the new place to look for it. It’s the same as the other commands, but using 302 instead of 301:
Redirect 302 / http://my.new.domain/
It’s a bit hacky as it is only supposed to be a temporary redirect. Indeed, you could argue that if you make a permanent redirect, it makes sense for the browser to cache it as permanent means it’s not going to change (and shouldn’t). But people are fickle and circumstances change. So, using a 302 will allow you to change your mind in the future, but will incur an overhead as the bounce to and from the server will always happen.
Hope that helps someone in their redirection wormholes.