Thursday, May 24, 2012

Generic htaccess redirect www to non-www


I would like to redirect www.example.com to example.com.



The following htaccess code makes this happen:




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



But, is there a way to do this in a generic fashion without specifying the domain name?


Source: Tips4all

11 comments:

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


    Same as Michael's except this one works :P

    ReplyDelete
  2. But if we need to do this for separate http and https:

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

    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    ReplyDelete
  3. Please see here:

    http://no-www.org/

    They've got code samples for apache.

    ReplyDelete
  4. Try this:

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


    If the host starts with www, we stick the whole host onto the start of the URL, then take off the "www."

    ReplyDelete
  5. RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^/(.*)$ http://%1/$1 [R]


    The RewriteCond captures everything in the HTTP_HOST variable after the "www." and saves it in %1. The RewriteRule captures the URL (sans leading "/") and saves it in $1.

    ReplyDelete
  6. There can be a lot of misinformation out there about htaccess redirects, I find. First off, make sure your site is running on Unix using Apache and not on a Windows host if you expect this code to work.

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


    (Make sure there are no line spaces between each line of text, though; I have added an extra space between lines so it renders okay in this window.)

    This is one snippet of code that can be used to direct the www version of your site to the http:// version. There are other similar codes that can be used, too.

    ReplyDelete
  7. Here are the rules to redirect a www URL to no-www:

    #########################
    # redirect www to no-www
    #########################

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


    Here are the rules to redirect a no-www URL to www:

    #########################
    # redirect no-www to www
    #########################

    RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
    RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]


    Note that I used NE flag to prevent apache from escaping the query string. Without this flag, apache will change the requested URL http://www.example.com/?foo%20bar to http://www.example.com/?foo%2250bar

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


    For subfolder

    ReplyDelete
  9. I am not sure why u want to remove www.
    But reverse version would be:

    # non-www.* -> www.*, if subdomain exist, wont work
    RewriteCond %{HTTP_HOST} ^whattimein\.com
    RewriteRule ^(.*)$ http://www.whattimein.com/$1 [R=permanent,L]


    And advantage of this script is:
    if u have something like test.whattimein.com or any other (enviroments for developing/testing)
    it wont redirect U to the original enviroment.

    ReplyDelete
  10. I used the above rule to fwd www to no www and it works fine for the homepage, however on the internal pages they are forwarding to /index.php

    I found this other rule in my .htaccess file which is causing this but not sure what to do about it. Any suggestions would be great:

    #

    always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/


    #

    never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l


    #

    rewrite everything else to index.php

    RewriteRule .* index.php [L]

    ReplyDelete