Skip to main content

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: Tips4allCCNA FINAL EXAM

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. The only way I got it to work after 30 minutes of struggling:

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

    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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex