Need Help - Htaccess - rewrite www to http

SuperBlackHat

Power Member
Joined
Feb 2, 2009
Messages
578
Reaction score
117
I want to make www.mydomain.com redirect to http://mydomain.com. Does anyone know how to do this properly? I tried:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ http://mydomain.com [R=301,L]

The browser told me:

Too many redirects occurred trying to open "http://mydomain.com/". This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

Does anyone know the right code to use?
 
Last edited:
After a bit of searching, I found the solution for anyone curious:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://mydomain.com/$1 [R=301,L]

Cheers
 
This is mine and it works fine. The only difference I can see it the RewriteBase / and the exclamation mark before condition?

RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^http://www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

EDIT: LOL, beat me to it.
 
This is mine and it works fine. The only difference I can see it the RewriteBase / and the exclamation mark before condition?

RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^http://www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

EDIT: LOL, beat me to it.

this is the correct solution.

your first example meant if the URI is not exactly http://www.mydomain.com then redirect... so will create a loop

your second example means if the URI starts with mydomain.com then redirect... so i don't see how it will work for http://www.mydomain.com

the example by Entrepreneur means if the URI starts with http://www.mydomain.com then redirect...

so really not sure how your working example actually works... having said that i am knackered and need to sleep :p
 
so are you saying my code is wrong? I tested it out and it seemed to be working fine
 
i just tried this and it didnt work - did nothing at all in fact... (domain name removed obviously)....

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xxxxxxxx.com
RewriteRule (.*) http://xxxxxxxx.com/$1 [R=301,L]

that's basically your example isn't it?
 
i dont know, when i test my site in the browser, it seems to work with no problems that i can see at least. the www redirects to http without a hitch.
 
ok this is odd.... if i type in www.mydomain.com in the browser, it stays there at www, without redirecting. Im trying entrepreneurs code and this is the result im getting.
 
ok this is odd.... if i type in http://www.mydomain.com in the browser, it stays there at www, without redirecting. Im trying entrepreneurs code and this is the result im getting.

exactly. your code won't do anything nasty if you dont put in the www but also wont do anything useful if you do put in the www.
 
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^mydomain\.com
RewriteRule (.*) http://mydomain.com/$1 [R=301,L]


This is the one i found to finally work.
 
Back
Top