Page 2 of 2 FirstFirst 12
Results 26 to 47 of 47
  1. #26
    Join Date
    Apr 2009
    Location
    USA / UK
    Posts
    4,577
    Quote Originally Posted by SpaceWalker View Post
    I wonder what's the point behind this, Why would you need to remove 'www.' from the site name ?
    Off the top of my head:

    1) Cookies

    2) Caching

    3) Search Engines

    I'm sure there are others
    RAM Host -- USA Premium & Budget Linux Hosting
    █ Featuring Powerful cPanel Shared Hosting
    █ & Premium Virtual Dedicated Servers
    Follow us on Twitter

  2. #27
    Thanks ;-)

  3. #28
    Thanks for the tutorial

  4. #29
    Join Date
    Jan 2010
    Posts
    40
    Thanks for the tut, it's really helpful!

  5. #30
    Quote Originally Posted by Dan L View Post
    PHP Code:
        $domain $_SERVER['SERVER_NAME'];
        
    $pathinfo $_SERVER['REQUEST_URI'];
        if(
    eregi('www.',$domain)) {
            
    $domain ereg_replace('www.','',$domain);
            
    header('Location: http://'.$domain.$pathinfo);
        } 
    That's all there is to it. Just put this code in your index.php file, and it will check whether the www. prefix is used or not. Everything from there is taken care of by the script itself.

    Enjoy
    PHP Code:
        $domain $_SERVER['SERVER_NAME'];
        
    $pathinfo $_SERVER['REQUEST_URI'];
        if(
    eregi('www.',$domain)) {
            
    $domain ereg_replace('www.','',$domain);
            
    header('HTTP/1.1 301 Moved Permanently');
            
    header('Location: http://'.$domain.$pathinfo);
        } 
    Due to crawlers it's better to put a 301 redirect, www is only a subdomain, very used, but can be seen as an other site.

  6. #31
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,201
    If you are using Apache as your web server, simply add the following to a .htaccess file in the same folder as your site pages

    Replace example.com with your domain name.

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]
    Cpanel/WHM • PHP • Perl • Ruby • Full Time Support
    LCWSoft - Canada web hosting (based in Newfoundland) since 2007
    Servers based in the US and Canada (Uptime Report)

  7. #32
    Join Date
    Apr 2009
    Location
    USA / UK
    Posts
    4,577
    Quote Originally Posted by anjo2 View Post
    PHP Code:
        $domain $_SERVER['SERVER_NAME'];
        
    $pathinfo $_SERVER['REQUEST_URI'];
        if(
    eregi('www.',$domain)) {
            
    $domain ereg_replace('www.','',$domain);
            
    header('HTTP/1.1 301 Moved Permanently');
            
    header('Location: http://'.$domain.$pathinfo);
        } 
    Due to crawlers it's better to put a 301 redirect, www is only a subdomain, very used, but can be seen as an other site.
    Excellent advise.

    However sending "HTTP/1.1 301 Moved Permanently" may not always work for a variety of reasons (apache vs something else, cgi vs fastcgi, linux vs windows, etc) -

    eregi and excess variables are unnecessary (and ereg is depreciated anyway).

    This is what I would use to ensure it all works as expected if you're using the PHP method:

    PHP Code:
    if(strstr($_SERVER['SERVER_NAME'],'www.')!=false)
    {
        
    header('Location: http://'.str_replace('www.','',$_SERVER['SERVER_NAME']).$_SERVER['REQUEST_URI'],true,301);

    That's far better on performance then firing up the regex engine , and sends a 301 redirect in the correct PHP manner

    Works with PHP 4.3.0+, PHP 5 (all, including 5.3.x), and PHP 6
    RAM Host -- USA Premium & Budget Linux Hosting
    █ Featuring Powerful cPanel Shared Hosting
    █ & Premium Virtual Dedicated Servers
    Follow us on Twitter

  8. #33
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,201
    I see 20 PHP-based solutions, when my simple .htaccess would be the best performance-wise and compatibility.
    Cpanel/WHM • PHP • Perl • Ruby • Full Time Support
    LCWSoft - Canada web hosting (based in Newfoundland) since 2007
    Servers based in the US and Canada (Uptime Report)

  9. #34
    Join Date
    Apr 2009
    Location
    USA / UK
    Posts
    4,577
    Quote Originally Posted by larwilliams View Post
    I see 20 PHP-based solutions, when my simple .htaccess would be the best performance-wise
    Agreed. If you're using Apache.

    and compatibility.
    Requires Apache (or LiteSpeed) - lots of people using Nginx, Lighttpd, IIS, and others these days - that doesn't work for them

    ---

    Really, this is something that should be configured at the web server daemon level rather than the site level - put that in httpd.conf and turn off .htaccess is you're using Apache and care about performance (although if you're still using Apache you prolly don't care much about performance anyway ).
    RAM Host -- USA Premium & Budget Linux Hosting
    █ Featuring Powerful cPanel Shared Hosting
    █ & Premium Virtual Dedicated Servers
    Follow us on Twitter

  10. #35
    With .htaccess is the better, but in the most cases php is more easy for all people, and works in "all" servers (the most servers have php).

  11. #36
    You would want to remove www. perhaps if you were calling that domain via code, keep your domains neat... otherwise for SEO someone above said that was a good reason to remove www., that seems a little silly to me...

  12. #37
    Join Date
    Dec 2007
    Location
    UK
    Posts
    951
    I never use the www prefix on domains, does this matter?
    Follow me on Twitter: @conrjac

  13. #38
    Quote Originally Posted by larwilliams View Post
    If you are using Apache as your web server, simply add the following to a .htaccess file in the same folder as your site pages

    Replace example.com with your domain name.

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

    It seems to be the easiest way for me instead of making changes to php files.


    but am not sure if it's going to make server loads higher because I added some VBSEO codes to my .hatacces file and it is making server loads high because of the rewriting rules , but I removed it along with the VBSEO plugin , I was lucky enough to figure out this before it's too late to remove the plugin

    I have been facing lots of troubles with www at the beginning of the domain.

    but when I logged in into my forums . The ( www ) disappeared


    any ideas ?


    Thank you for sharing

  14. #39
    This stuff helps with seo, although i prefer to change domainname.com to www.domainname.com Maybe it is better the other way.

  15. #40
    Quote Originally Posted by aspire7 View Post
    This stuff helps with seo, although i prefer to change domainname.com to www.domainname.com Maybe it is better the other way.
    It's the same.

  16. #41
    Join Date
    Nov 2005
    Posts
    282
    Its preference whether you want to have your website mainly hosted on www.example.com or just example.com as long as you have valid dns records for both you can redirect one to the other. Or as many different subdomains you want. The only thing is that when people manually enter your web address they are more likely to type www.example.com than just example.com, and it might reduce the load on your server by a few milliseconds if your webserver operated off of www and just forwarded the empty subdomain. Any time you forward with an header it means the remote client will have to send another GET request to the server, which could mean for them (depending on their bandwidth, and line quality) a noticable difference in load times.

    And hey look at that this web forum will automatically parse www.example.com into a link while it does not parse example.com into a link. Things like this can make forcing to www more advantagous.

  17. #42
    Join Date
    Apr 2011
    Posts
    32
    Can you extend the script a bit? Like make it query a DB and see where the DB tells www to go to, That would make it easier for creating subdomains.

  18. #43
    This work with www due to the most servers have a type A for www
    Code:
    www A 127.0.0.1
    When you create a subdomain, you create a dns entry
    Code:
    subdomain A 127.0.0.1
    It is not easy at all...

  19. #44
    thanks for the informative post

  20. #45
    Or you can put it in your .htaccess file.

  21. #46
    nice to know this php code for this purpose. i only knew .htaccess way.

  22. #47
    You can achieved this with the use of Apache's URL rewrite. :-)
    EmailHosting.com
    http://www.emailhosting.com

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •