Forum Moderators: coopster

Message Too Old, No Replies

Redirect with PHP to lowercase URLs

         

harmhero

10:33 am on Oct 8, 2011 (gmt 0)

10+ Year Member



For SEO purposes (getting rid of duplicate content) I'm converting all my links to lowercase, since in Google's webmaster tools I came accross duplicate pages like:

www.domain.com/Foo
and
www.domain.com/foo

I was just wondering if this script what I found is accurate (it works, but I want to know if the 301 is correct):


if (preg_match('/[A-Z]/', $_SERVER['REQUEST_URI'])) 
{

$lowercase_file_url = strtolower("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);


header("HTTP/1.1 301 Moved Permanently");
header("Location: $lowercase_file_url");

exit();
}


In the original script I found, this rule:

header("HTTP/1.1 301 Moved Permanently");

was:

header("HTTP/1.1 303 See Other");

But I think the 301 version is better(?).

g1smd

5:49 pm on Oct 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't use
$_SERVER['HTTP_HOST']
here. Hard code the domain name.

If you don't do this, example.com/Foo will redirect to example.com/foo and www.example.com/Foo will redirect to www.example.com/foo and you continue to promote Duplicate Content.

Don't fool yourself that this isn't a problem because you also have a non-www to www redirect in the configuration. That only makes things worse because a non-canonical upper case request will trigger an unwanted multiple step redirection chain.

Hard code the domain in the PHP code.

And, yes, use 301 not 303.

harmhero

6:33 pm on Oct 8, 2011 (gmt 0)

10+ Year Member



...but I have several subdomains. You think its wiser to
for example:

$lowercase_file_url = strtolower("http://" . $subd . "mydomain.com" . $_SERVER['REQUEST_URI']);

g1smd

7:09 pm on Oct 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If there are multiple sites being hosted, you'll need to detect
$_SERVER['HTTP_HOST']
and then make sure you strip - or add - the www if it is present - or not present - as appropriate.

It's just a few new lines of PHP code ahead of your existing code.

harmhero

7:48 pm on Oct 8, 2011 (gmt 0)

10+ Year Member



probably something like:

$explode = explode(".", $_SERVER['HTTP_HOST']);
$domain = $explode[0];

coopster

2:07 pm on Oct 14, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It really depends on how you have your Apache VirtualHost configuration set up.

If you have multiple VirtualHost containers, one for each subdomain, you would be fine -- as long as each subdomain has it's own unique content of course. And that goes for the example.com versus www.example.com. The "www" is a subdomain and if it has different content than the example.com domain, you are fine.

Now, as g1smd mentioned, if your example.com and www.example.com are the same domain name, you need to handle accordingly. I prefer to do so in the Apache configuration by setting a VirtualHost container for each. In the non-canonical you can issue a permanent redirect to the canonical in just a few short lines. Example:

<VirtualHost 127.0.0.1:80> 
ServerName example.com:80
RedirectMatch permanent (.*) http://www.example.com$1
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost 127.0.0.1:443>
ServerName example.com:443
SSLEngine on
SSLVerifyClient none
SSLCertificateFile /path/to/server.crt
# required on Windows ...
#SSLCertificateKeyFile /path/to/server.key
RedirectMatch permanent (.*) [example.com$1...]
</VirtualHost>
</IfModule>