Welcome Guest, Not a member yet? Register   Sign In
301 Redirect for search engine optimization
#1

[eluser]Kamil Rudnicki[/eluser]
This code redirects site without 'www.' to URL with 'www.'. It helps with search engine optimization:

Code:
if(strpos($_SERVER['HTTP_HOST'], 'www.' ) === false)
{
            Header( "HTTP/1.1 301 Moved Permanently" );
            Header( "Location: ".site_url($_SERVER['REQUEST_URI']));
            exit();
}
#2

[eluser]skunkbad[/eluser]
Why not use .htaccess ?
#3

[eluser]BrianDHall[/eluser]
I like it, thanks for sharing. Good for those without htaccess or on servers without such functionality, or just those who don't want to touch their htaccess for fear of screwing it up Wink
#4

[eluser]hugle[/eluser]
[quote author="skunkbad" date="1261989530"]Why not use .htaccess ?[/quote]

Hi Smile
can you give .htaccess example?Smile
#5

[eluser]skunkbad[/eluser]
[quote author="hugle" date="1262003641"][quote author="skunkbad" date="1261989530"]Why not use .htaccess ?[/quote]

Hi Smile
can you give .htaccess example?Smile[/quote]

Example (replace mywebsitedomain with your own):
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mywebsitedomain\.com [NC]
RewriteRule ^(.*) http://www.mywebsitedomain.com/$1 [R=301]

If you can use this, the benefit would be that Apache (the web server) would perform the redirect without PHP processing a script. I think theoretically this means a faster redirect/response. Like Brian said, you might not be able to use .htaccess files, so this might not work for you. Most of my sites are powered by LiteSpeed, and I use .htaccess, so using .htaccess isn't exclusive to Apache, but the vast majority of web servers out there are Apache.
#6

[eluser]Kamil Rudnicki[/eluser]
[quote author="skunkbad" date="1261989530"]Why not use .htaccess ?[/quote]

I have serveral pages where I don't want to have 301 redirect.




Theme © iAndrew 2016 - Forum software by © MyBB