2 Comments on
Creating HTTP 301 Permanent Redirects with .htaccess
Creating HTTP 301 Permanent Redirects with .htaccess
If a company owns multiple domains its website, its Google PageRank can be split up among those domains. As you may be aware, Google establishes relevancy in part through links which point to a page. Here’s a made-up example using Chiquita Banana…
Suppose that someone owned both “chiquita.com” and “chiquita-banana.com” and that half the incoming links pointed to the former domain while half the incoming links pointed to the latter domain (all hypothetically using the link text “banana”). By doing so, Google thinks that the two servers are distinct from each other and the PageRank for the term “banana” is half as strong as it could be (since it’s divided among two domains).
The solution is to setup redirects from any “secondary” domains to point to the “primary” domain. And, if the server-side logic sends an “HTTP 301” response when doing so, Google accumulates the PageRank from any “secondary” domains and combines that with the PageRank of the “primary” domain :).
Here’s how you can go about that — it’s a matter of adding a few lines to a site’s .htaccess file in its root directory (or creating the file if it doesn’t already exist). Naturally, this technique only works on Apache.
Here’s the basic syntax:
Redirect permanent /old-directory/ http://www.example.com/new-directory/
A couple notes:
-
The first filename (“/old-directory/” above) isn’t allowed to have any “http://” bits while the second filename (“http://www.example…” above) is required to have the “http://” bits.
-
Above all, the single thing to keep in mind is that these redirects apply for all subdirectories — Apache applies the redirect to all URLs which contain the “starting point” string. So, in order to establish a redirect to point all /delicious-ice-cream/* requests to /ice-cream/*, you would use this line:
Redirect permanent /delicious-ice-cream/ http://www.example.com/ice-cream/Or, put another way, the following are some of the redirects covered by that line:
-
/delicious-ice-cream/rocky-road/ → /ice-cream/rocky-road/
-
/delicious-ice-cream/rocky-road/ingredients/ → /ice-cream/rocky-road/ingredients/
-
/delicious-ice-cream/french-vanilla/ → /ice-cream/french-vanilla/
-
-
If case you’re wondering, yes, you could redirect a whole site this way. Suppose that you owned both brownies.com and chocolate-brownies.com. One way to redirect all pages from the latter to the former would be to put this single line in the .htaccess file on the root of chocolate-brownies.com:
Redirect permanent / http://www.brownies.com/ -
Though the examples above use directory names, this works just as well with filenames (should the need arise). For example:
Redirect permanent /foo/bar/filename.html http://www.example.com/abc/filename.html -
Suppose you need to redirect a particular directory but not its subdirectories? Let’s suppose that I needed to redirect domainname.com/products/ to domainname.com/products/earl-grey/ (since there wasn’t actually a page in the /products/ directory.
I couldn’t just use a regular redirect with “/products/” since that would hypothetically match all the English Breakfast pages as well (among others) — I wouldn’t want those redirected to the Earl Grey section. In those cases, a regular “Redirect” wouldn’t work, but Apache also offers “RedirectMatch” which is basically the same type of thing but with regular expression support (for more precise matches).
-
In case you’re curious, Apache also accepts other values for that second parameter — which is set to “permanent” for our use (which sends HTTP 301) — that could be used if you wanted to signify, for instance, a temporary redirect.
-
Lastly, if you want to check that your redirects are sending the right HTTP response codes, you can load the page with WebSniffer.net. There, you can enter a URL and it’ll show you its HTTP request & response headers.
And, just for the sake of completion, you can also create HTTP 301 redirects in PHP and ASP if you had to; that approach could be used if you had write access to a site but mod_alias wasn’t available for one reason or another.
Gozar said:
Great page with all the explanations.
In my case, I have a problem where on my hosting account I had two domains such as http://www.ozarweb.com and http://www.ozar.net the former initially set as the primary domain. Last week I switched the primary and the secondary and now I need to redirect all incoming requests to ozarweb.com and ALL SUBDIRECTORIES to ozar.net/{same subdirs}.
However using
Redirect permanent / http://www.ozar.net/
does not appear to be a valid solution in my case, because if I put it in my .htaccess file - since it is the same file which is used by the old and the new domain name at the same time, the direct requests to the new domain could be redirected to itself causing an infinite blind loop or so I fear.
Can you suggest a different redirect rule syntax with regular expressions which would help me solve the issue?
:: 13 Feb 2008 at 9:17 am ::
Annonces said:
Thanks for explanation.
:: 15 Apr 2008 at 10:14 am ::