20

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?

Yeti
  • 5,628
  • 9
  • 45
  • 71

5 Answers5

56

Here’s a more generalized solution:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 5
    Is there a way to detect and rewrite to the same URL scheme (http or https) from which a user came? `http://www.example.com/` to `http://example.com/` and `https://www.example.com/` to `https://example.com`. – Stephen Watkins Sep 11 '12 at 23:52
  • Does the REQUEST_URI contain index.php? For some reason when I go www.example.com, it redirects to www.example.com/index.php – CMCDragonkai Dec 03 '13 at 08:12
  • 1
    @CMCDragonkai Don't forget the QSA flag if you want to append the original query string. – jordanbtucker Feb 06 '15 at 18:33
28

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 Rosendo
  • 25,001
  • 7
  • 80
  • 118
  • 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 Mar 02 '10 at 07:38
  • 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 Rosendo Mar 02 '10 at 07:40
  • 1
    +1 for virtualhost approach. Don't resort to mod_rewrite until you really need to. – bobince Mar 02 '10 at 07:40
  • 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 Mar 02 '10 at 07:46
  • Great, I've edited in th virtual host stuff as well if you need it. – Kyle Rosendo Mar 02 '10 at 07:55
  • I suggest you remove the "10.0.0.1" bit by "*" for a more portable solution. – pixeline Jan 28 '15 at 22:08
  • is the `QSA` flag necessary since theres `$1`? my understanding is that the `1` refers to whatever query string might be present? – oldboy Aug 09 '19 at 06:05
10

As a minor tweak of Kyle's answer, I'd put a / in the RewriteRule match condition, like

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

Otherwise, you get a double slash as a result.

http://www.example.com/smth -> http://example.com//smth
Fredrik Boström
  • 1,499
  • 1
  • 19
  • 22
6

I would always use 307 (temporary redirect) first because if you get it wrong some browsers cache it permanently. I ended up installing Google Chrome just because I couldn't get my Firefox to forget a bad redirect even when I deleted the whole cache.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
4

Here is a solution if you don't want a hard coded domain name. Don't forget to start the rewrite engine or this won't work!

 # Start rewrite engine
 <IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
 </IfModule>

 # Rewrite "www.example.com -> example.com"
 <IfModule mod_rewrite.c>
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
 </IfModule>
seaBass
  • 585
  • 1
  • 6
  • 17