GuruGary
asked on
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.
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"
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.
If the above will not work
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI } [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI
ASKER
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.
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.
Yes, I was wrong (wrote it without testing). Sorry. The second try should work.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^%{SERVER_NAME} [NC]
RewriteRule ^(.*) %{REQUEST_METHOD}%{SERVER_