PDA

View Full Version : www and non-www urls


yetoso
05-23-2005, 02:59 PM
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?

kccricket
05-23-2005, 09:31 PM
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:

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:

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
05-23-2005, 10:39 PM
Yeah, you have to add (to have a www-less domain):
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.

yetoso
05-24-2005, 04:30 AM
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

Ray
05-27-2005, 10:30 AM
As a note, you could also accomplish this by adding the same directives to a .htaccess file.

yetoso
05-27-2005, 12:02 PM
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

kccricket
05-27-2005, 01:34 PM
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).