SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Location
    Washington, D.C.
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    www and non-www appear differently--how to fix?

    My website appears differently depending on whether I view it as "www.wirestrungharp.com" or simply "wirestrungharp.com"
    The first one of these, the www, appears as I want it to; the other one does not.

    My initial question is why and how does this happen? Why are there two versions of my website?

    Then, how do I fix it? I suspect I need to do a "ReDirect" in my .htaccess file. However, in very many of the site's pages I have given locations of links within the site as "http://wirestrungharp.com/desired__link" which defaults to "wirestrungharp.com/desired_link" which of course displays incorrectly. Must I go through all these pages and change the internal links to include the "www"?

    Thank you in advance for any help or suggestions!

  2. #2
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,892
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by WireStrung View Post
    My website appears differently depending on whether I view it as "www.wirestrungharp.com" or simply "wirestrungharp.com"
    The first one of these, the www, appears as I want it to; the other one does not.

    My initial question is why and how does this happen? Why are there two versions of my website?

    Then, how do I fix it? I suspect I need to do a "ReDirect" in my .htaccess file. However, in very many of the site's pages I have given locations of links within the site as "http://wirestrungharp.com/desired__link" which defaults to "wirestrungharp.com/desired_link" which of course displays incorrectly. Must I go through all these pages and change the internal links to include the "www"?
    Web servers are not automatically configured the same way, and the specs don't require example.com and www.example.com to point to the same page. Most servers are set up to have them both giving the same page, but they are still represented as two separate URLs.

    Fortunately, it's very easy to solve this. In your .htaccess file, you just need some code along the lines of:
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]
    except that, obviously, you would replace example.com with your own domain name. This means that anyone going to the non-www version of the page is automatically redirected to the www version.

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Location
    Washington, D.C.
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thank you, that did the trick and I don't have to dip into every page I've coded to make the change!

    Thank you also for the explanation. Good to know.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Aw, Stevie, you KNOW better than this!

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

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
    The use of example.com needs to have the dot character escaped (to specify the dot character rather than a metacharacter representing ANY character) and, because {HTTP_HOST} is not case sensitive, it requires the use of the No Case flag.

    As for creating a new variable when you already have the {REQUEST_URI} available, IMHO, that's a waste of CPU resources. Best to just get to the redirection with the {REQUEST_URI} (which also sorts out the question of Apache 1.x or 2.x with the leading /).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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