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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.