Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do I add a 301 redirect to my Apache httpd.conf?

I just checked and apparently both the "domain.com" and "www.domain.com" work for my site, bringing up the same page with different URLs. According to an SEO buddy of mine, that's a bad thing. Is it? And if so, how can I tweak my web server configuration to fix it?


Dave's Answer:

This is a bad thing, as your friend suggests. The reason? Every URL is considered as a different page on your site according to the search engines, so while you might deserve, say, a combined PageRank on Google of five, it might be that "hostname.com" is a PR3 and "www.hostname.com" is a PR4 when you check.

You can quickly ascertain whether your site has this problem too, by simply entering in both versions of the domain name to your Web browser and checking to see if they stay as you typed them in (bad) or one of them is magically rewritten to be in the other form so that there's a single, consistent URL for both (good).

If you need to tweak this, as I had to on this site recently, you can most easily do so by going into your httpd.conf file and adding the following three lines:

<VirtualHost askdavetaylor.com>
  Redirect 301 / http://www.askdavetaylor.com/
</VirtualHost>

In this instance, I want the form "www.askdavetaylor.com" as the default consolidated domain name format. You can test it: click on this link and watch what URL you actually have in your Web browser by the time you get the home page: http://askdavetaylor.com/.

Depending on your Apache configuration, you might need a more sophisticated version of this too:

<VirtualHost 70.84.11.36>   ServerName askdavetaylor.com
  RewriteEngine on
  RewriteRule ^/(.*) http://www.askdavetaylor.com/$1 [L,R=301]
</VirtualHost>

The difference between the two is that the latter only works if this is the only domain that is assigned the specified IP address, while the former works if you have multiple domains with the same IP address, as I do on my own server.

Also note that while the Apache Rewrite Engine is darn cool and very powerful (I use it for lots of tricks on this site) it's also slow, so engaging it when you don't really need to use it can slow down your Web server. Not a good thing.

Finally, it's important that you use a 301 permanent redirect rather than 302 temporary redirects so as not to get into trouble with Google and other search engines. Google's Matt Cutts has a worthwhile article on this subject if you want to know why this is so: The little 301 that could.

If you want to know more about redirects and error pages, by the way, you might well find the site custom 404 error page amusing and useful.

Thanks to Steve Loyola of Best Book Buys.com and Alek Komarnitsky, who is busy trying to raise money for charity through the weird and wacky V7ndotcom elursrebmem competition, for their assistance with this important server note.


More Useful HTML, JavaScript and Web Site Programming Articles:
✔   How to Create Predefined Google Image Search Links?
Thanks for the Amazon URL [see Creating Amazon Search Links]. That worked beautifully. In fact, I sent you $5.00 for coffee in thanks....
✔   Can I embed a Facebook search box on my blog site?
I've seen your articles about how to add a Twitter or Google search box on a Web page, but I have a tougher...
✔   Can I use CSS for drop shadows on my blog?
I want to give my site a bit of a facelift and add some neat graphical elements. One of which is drop shadows....
✔   How can I embed interactive photo panoramas on my site/blog?
I read through your blog entry about how to take panoramic photos with iOS 6 and an iPhone 5 and got enthused. I've...
✔   How can I create a Twitter search URL shortcut?
I'd like to add a few Twitter search links to my Web site. Is that possible, or does Twitter prohibit this sort of...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 10

Mr. Christmas Lights said, on March 19, 2006 5:46 PM:

Nice writeup Dave and great explanation of a technical topic that is useful for webmasters.

BTW, I know that Alek guy and he can be pretty wierd and wacky ... he's always up to something entertaining! ;-)

physio said, on March 20, 2006 9:16 AM:

Thanks for the information. Can you also mention how to set this redirection up on IIS for those of us paying for hosting with companies that don't use Apache?

Dave Taylor said, on March 20, 2006 9:38 AM:

Physio, that's documented at the http://www.404-error-page.com/ site. Check it out!

Pat Riley said, on April 7, 2006 9:51 PM:

Dave, thanks for your very informative information on yourdomain.com to www.yourdomain.com 301 redirects through modifications to the httpd.conf file. I tested your approach on my server and it works exactly as advertised.

Question: I've been using the following .htaccess file rewrite instruction to accomplish (in theory) the same thing:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=permanent,L]

Could you commment on the validy of my method versus modifying the httpd.conf file? Do both approaches generate exactly the same end result? Would one be more efficient than the other?

It seems to me that the httpd.conf approach would send a site visitor back to the Web and through all the domain name server lookups a second time to get bach to the www version of the URL. Is this true and would this basically slow everything down versus simply rewriting the address through the .htaccess directive?

Thanks for your thoughts.

Pat Riley

Erik said, on February 5, 2007 1:52 PM:

Are you using "former" and "latter" correctly?

Former would be:

Redirect 301 / http://www.askdavetaylor.com/

You say "the former works if you have multiple domains with the same IP address"

I think you've got it reversed.

Glenn said, on April 8, 2008 6:51 PM:

Just a warning! A cut and paste of Dave Taylor's second bit of code will actually redirect your website to www.askdavetaylor.com :)

Yeah, I know that's obvious. But trust me, it's a bit startling to see type in your domain name and end up at dave's website :). D'Oh!

Suresh said, on September 29, 2009 9:35 AM:

Wow, such simple instructions, and it worked first time. Many thanks Dave, I owe you a cuppa!

P.S. to check the http headers use:

http://www.seoconsultants.com/tools/headers.asp#Results

Jean-Michel Raux said, on November 25, 2009 1:39 PM:

I have found your solution for multiple domains on one IP not working on apache 2.2.11. The virtual hosts overlap each other and some are ignored.

Instead, I have created one virtual host per redirect target as follow:

VirtualHost *:80
ServerName target-domain.com
ServerAlias domain1.com
ServerAlias www.domain1.com
ServerAlias domain2.com
ServerAlias www.domain2.com
Redirect 301 / http://www.target-domain.com/
/VirtualHost

VirtualHost *:80
ServerName target-domain2.com
ServerAlias domain3.com
ServerAlias www.domain3.com
ServerAlias domain4.com
ServerAlias www.domain4.com
Redirect 301 / http://www.target-domain2.com/
/VirtualHost

Add the <> around the Virtualhost lines, for some reason it was removed from the comment

Jeremy said, on December 20, 2011 12:19 PM:

When I use the first method

Redirect 301 / http://www.askdavetaylor.com/
in httpd.conf I get a redirect loop.

Second method doesn't redirect my site at all.
I have searched the web and tried many different methods but cant get it to redirect with a loop.

Here is the httpd.conf file:

Order allow,deny
Allow from all
DirectoryIndex index.php
AllowOverride None
AddDefaultCharset Off

php_value memory_limit 228M
php_value max_execution_time 18000
php_flag magic_quotes_gpc off
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
php_flag zend.ze1_compatibility_mode Off
php_flag zlib.output_compression on

RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d;=$2
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

SecFilterEngine Off
SecFilterScanPOST Off

ExpiresDefault "access plus 1 year"
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary


SSLOptions StdEnvVars

ExpiresActive On
ExpiresDefault A0

ExpiresDefault A29030400
Header append Cache-Control "public"

ExpiresDefault A1209600
Header append Cache-Control "public"

ExpiresDefault A604800
Header append Cache-Control "proxy-revalidate"

ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"

***your website comment area automatically removes some characters so tags are gone.

Ryan said, on September 22, 2012 11:31 PM:

I am trying to do something similar, but not exactly this and wodnering how it would be accomplished.

I am moving my www.site.com to www.site2.com and putting something else on www.site.com. Is there a way to redirect any 404 pages for www.site.com to www.site2.com? So, if I type in www.site.com/info.html which used to exist but not longer exists, I want it to go to www.site2.com/info.html where it does exist. There are A LOT of dynamically created pages, so I can't name each page individually.

Thanks for any help - I haven't been able to find this type of information anywhere on the web.

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.