wright67uk Posted July 10, 2011 Share Posted July 10, 2011 Im having trouble with my site listing in google. I have every page of my site listed in google but only for i-stevenage.co.uk I want google to list www.i-stevenage.co.uk instead. I believe this is called canonicalization!?! If i were to put the below code into my site pages, would it do the job? if yes, then where would I place it? would it have to be the first line of my code? if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); } and for my index page... if ($_SERVER['REQUEST_URI'] == '/index.php') { header( 'HTTP/1.1 301 Moved Permanently' ); header( 'Location: http://www.i-stevenage.co.uk/' ); } Quote Link to comment https://forums.phpfreaks.com/topic/241583-canonicalization/ Share on other sites More sharing options...
per1os Posted July 10, 2011 Share Posted July 10, 2011 Honestly, and this is just my opinion, I would not even care about the www. It is just 4 extra items people have to type and there is no benefit to having www. I made that mistake with my site and I wish I never did the www. Just my two cents. Instead of doing that in PHP, use a .htaccess, this will require mod_rewrite to be enabled: RewriteEngine On RewriteCond %{HTTP_HOST} ^i-stevenage.co.uk RewriteRule ^(.*)$ http://www.i-stevenage.co.uk/$1 [R=permanent,L] That should get you where you want to be without having to mess with the PHP side of it. Quote Link to comment https://forums.phpfreaks.com/topic/241583-canonicalization/#findComment-1240865 Share on other sites More sharing options...
wright67uk Posted July 10, 2011 Author Share Posted July 10, 2011 What mistake did you make? Im not so concerned about what the user types in, I just want all of my visitors, links, google ranking to go to www.i-stevenage.co.uk opposed to i-stevenage.co.uk, but yes I see what your saying on the user side of things. Im not sure if mod_rewrite is enabled as godaddy only allow access to it via linux hosted sites and im windows hosted. I havent put the if server script on my site yet, and now im curious of what effects it had on your site!?! Quote Link to comment https://forums.phpfreaks.com/topic/241583-canonicalization/#findComment-1240870 Share on other sites More sharing options...
per1os Posted July 10, 2011 Share Posted July 10, 2011 www. is not any seo friendlier, just an FYI I do not think it makes much difference, other than your URL will always be 4 characters shorter no matter what. If you want to test it and do not know if you have mod_rewrite enabled you can wrap it in an if statement: <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^i-stevenage.co.uk RewriteRule ^(.*)$ http://www.i-stevenage.co.uk/$1 [R=permanent,L] </IfModule> This way your site won't break if mod_rewrite is not on. To test if it is working, you just have to goto the non-www version of your site and it should 301 redirect you to the www. version. Quote Link to comment https://forums.phpfreaks.com/topic/241583-canonicalization/#findComment-1240876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.