• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1363
  • Last Modified:

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.
0
GuruGary
Asked:
GuruGary
  • 4
  • 3
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
ravenplCommented:
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now