0

Based on what I read on Apache I used the following example they provided to do a 301 Redirect on all my web sites.

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

This is not quite working as they said it would. If I try www.domain.com it works. If I try domain.com I get www.domain.com//home/www/public_html/www.domain.com

Looks like it wants to include the DOCUMENT_ROOT in the redirect. Am I better off to create an individual .htaccess for each web site?

What is faster to run - Apache or HTACCESS?

Tim
  • 403
  • 1
  • 6
  • 20

2 Answers2

0

Using Apaches httpd.conf is faster since accessing the .htaccess file adds a small overhead—Apache checks every directory—and parent directory—for the file and it will be loaded for every request.

Using the httpd.conf is better when you have access to it. Use .htaccess if you don't have access to the main configuration file.

  • Will stick with `httpd.conf` I do have access. Any reason why it would include the DOCUMENT_ROOT? – Tim Feb 17 '12 at 15:34
  • You need to add `RewriteBase /` or a leading `/` if you're using redirects not inside a `` block. –  Feb 17 '12 at 16:05
0

Try this instead. Make sure you include the RewriteBase /

RewriteEngine on
RewriteBase / 

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you still get the old result, your previous 301 redirect is probably cached, retest in Private (Incognito) Browsing Mode.

Community
  • 1
  • 1
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
  • That `RewriteBase /` was the missing ingredient. Thank you for pointing this out. – Tim Feb 17 '12 at 16:49