+ Reply to Thread
Results 1 to 7 of 7

Thread: www and non-www urls

  1. #1
    Junior Member
    Join Date
    May 2005
    Posts
    3

    Default www and non-www urls

    Okay, here's my issue.

    I am running OsCommerce on my site (gridchoice.com)
    my site is accessible through both www and non-www urls. for example: gridchoice.com and www.gridchoice.com

    Although, when you click through to another page it will then remove the www. So you could type in http://www.gridchoice.com/catalog and it would display as such until you click another link.. so it defaults to non-www.

    This is a problem, however, when a search engine spider manages to index your site both with and without the www, because then it counts them as seperate sites and supposes it's repeat content. which it will look unfavorably upon.

    SO, I want to permenantly 301 redirect all url's to their www equivilant. for example, if somebody typed: http://gridchoice.com ... it would redirect to http://www.gridchoice.com

    also, it would apply to all urls everywhere.. so if somebody typed http://gridchoice.com/catalog/shipping.php then it would redirect to http://www.gridchoice.com/catalog/shipping.php respectively.

    I have tried numerous times to do this numerous ways only to be unsuccessful. is there something about our particular webhosting that I must take into consideration?

  2. #2
    Junior Member
    Join Date
    May 2005
    Location
    North Carolina, USA
    Posts
    23

    Default

    If you can find a way to do it, just add the following peice of PHP code to your scripts so that it's loaded on every page and before any content is sent to the browser:

    Code:
    if ( substr( $_SERVER['HTTP_HOST'], 0, 4 ) != 'www.' ) {
    	header( 'HTTP/1.0 301 Moved Permanently' );
    	header( 'Location: http://www.' . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'] );
    }
    Keep in mind that if you do this, you will be redirecting every one of your users every time they click on a link (if what you say is true). I would recommend reversing it. Redirecting all users to a non "www" URL:

    Code:
    if ( substr( $_SERVER['HTTP_HOST'], 0, 4 ) == 'www.' ) {
    	header( 'HTTP/1.0 301 Moved Permanently' );
    	header( 'Location: http://' . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'] );
    }
    I'm gonna poke around the apache documentation. I think there's a way to do this with mod_rewrite.
    -kccricket
    * chirp * chirp *

  3. #3
    Junior Member
    Join Date
    May 2005
    Location
    North Carolina, USA
    Posts
    23

    Default

    Yeah, you have to add (to have a www-less domain):
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^example\.net [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/(.*) http://example.net/$1 [R=301,L]
    To your VirtualHost directive. As a shared hosting customer, I don't think you can do that. However, Steadfast might be willing to add it for you.
    -kccricket
    * chirp * chirp *

  4. #4
    Junior Member
    Join Date
    May 2005
    Posts
    3

    Default

    thank you a ton kccricket, you are very helpful.

    I will contact customer support with what you have given me. I'll let you know

  5. #5
    Ray
    Ray is offline
    I can't do everything.
    Join Date
    Jun 2004
    Location
    Plymouth, WI
    Posts
    134

    Default

    As a note, you could also accomplish this by adding the same directives to a .htaccess file.
    Ray Tetzloff


  6. #6
    Junior Member
    Join Date
    May 2005
    Posts
    3

    Default

    Quote Originally Posted by Ray
    As a note, you could also accomplish this by adding the same directives to a .htaccess file.
    hey yeah, I tried to do that but I couldn't get it to work. I placed variations of this:

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

    but when analyzed with a redirect checker it was trying to redirect to something like this (from memory):

    http://example.net//hsphere/root/example.net/

    something like that.. any ideas?

    Also.. on a side note: I have a 301 redirect from: example.com to example.com/catalog

    now, do I keep my robots.txt file at the root or do I need to move it to the catalog folder?

    and if I keep it in my root, do I keep all the paths the same (in the robots.txt file) or do I need to rewrite them as if the catalog was the root since the robot will be redirected there?

    for example, I want to disallow gridchoice.com/catalog/products_new.php from being indexed... how would I write the disallow path in the robots file and where would I put it when considering the 301 redirect I have in place?

    thanks

  7. #7
    Junior Member
    Join Date
    May 2005
    Location
    North Carolina, USA
    Posts
    23

    Default

    Quote Originally Posted by Ray
    As a note, you could also accomplish this by adding the same directives to a .htaccess file.
    I had originally tried it in .htaccess, but it didn't work for me. I was told by the folks in #apache on freenode that it specifically would not work in a .htaccess.

    But maybe that's just how it is with a default setup (i.e. my VPS).
    -kccricket
    * chirp * chirp *

+ Reply to Thread

Posting Permissions

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