views:

48

answers:

2

I'm using the following to try and remove WWW from the url:

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://example.com$1 [R=301]

But for some reason it doesn't work. Any suggestions?

+2  A: 

Try:

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

And without mod_rewrite:

<VirtualHost 10.0.0.1:80>
        ServerName www.example.com
        Redirect permanent / http://example.com/
</VirtualHost>

Virtual hosts can be used by completing the steps in the following URL: Setting Up A Virtual Host in Apache.

Kyle Rozendo
This is what happened: The webpage at http://www.example.com/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. *I removed these but the site is still down! Why so??*
Yeti
Question 1. Did you use both at the same time? Question 2. Did you change the IP address to the correct value? Question 3. Do you own example.com?
Kyle Rozendo
+1 for virtualhost approach. Don't resort to mod_rewrite until you really need to.
bobince
Ans 1: Yes. I've removed both now and the site's up. Ans 2: I have no clue where to add/remove virtualhost. Ans 3: I'm using example.com instead of my website. Can you pls tell me more about virtualhost and where can I make the change? I have no clue.. been only using mod_rewrite.
Yeti
The RewriteRule above worked! Thanks.
Yeti
Great, I've edited in th virtual host stuff as well if you need it.
Kyle Rozendo
A: 

Here’s a more generalized solution:

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