Apache RewriteRule to add www without losing trailing file or path info

I have an Apache web server that uses virtual hosting that  I would like a rewrite rule for.  

Scenario 1:
Simple virtual hosting, and I would like
http://domain.com/whatever
redirected to
http://www.domain.com/whatever
(where the directory "/whatever" is not necessarily static and could be "/" or "/dir1" or "/dir2" or "/something.php?var=value" as long as the trailing file or path info stays the same in the rewritten URL)

Scenario 2:
Virtual hosting with 1 or more aliased domains, and I would like any of:
http://primarydomain.com/whatever
or http://secondarydomain.com/whatever
or http://www.secondarydomain.com/whatever
redirected to
http://www.primarydomain.com/whatever

If scenario 1 and scenario 2 need different rewrite rules, that is fine.  I just spent about 30 minutes looking over mod_rewrite and the more I read, the more I decide this would take too long to figure out on my own.
LVL 10
GuruGaryAsked:
Who is Participating?
 
ravenplConnect With a Mentor Commented:
Oh, that's strange (this time I tested it on IE/FF) and it worked for me.
Try (maybe %{SERVER_NAME} and www.domain.com are not the same, as they should!)
RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
 RewriteRule .* http://www.domain.com%{REQUEST_URI} [R,L]
0
 
ravenplCommented:
#untested
Options +FollowSymlinks
RewriteEngine On
 RewriteCond %{HTTP_HOST} !^%{SERVER_NAME} [NC]
 RewriteRule ^(.*) %{REQUEST_METHOD}%{SERVER_NAME}%{REQUEST_URI} [R=301,QSA,L]
0
 
ravenplCommented:
If the above will not work

RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
 RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GuruGaryAuthor Commented:
Hi, ravenpl.  I tried it, and it rewrites with
http://domain.com/path/GETdomain.com/path/GETdomain.com/path/GETdomain.com/path/GET (repeats several more times).

I tried taking off the REQUEST_METHOD, and it removes the GET, but still repeats 21 times.
0
 
ravenplCommented:
Yes, I was wrong (wrote it without testing). Sorry. The second try should work.
0
 
GuruGaryAuthor Commented:
You were too quick ... I guess you replied with your 2nd code while I was still testing.  Well, I tried the 2nd commands and FireFox gave me:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
0
 
GuruGaryAuthor Commented:
Perfect!  I guess it was the SERVER_NAME?  Maybe has to do with the virtual hosting?  Anyway hard coding the domain works fine.  Thanks for the quick response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.