Forum Moderators: phranque

Message Too Old, No Replies

Canonical issues

Google not showing domain.com/ first

         

wheelie34

4:13 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there anything wrong with the code below, Google isn't showing the example.co.uk/ as the first site: result although the code seems to work as expected.

----------------------
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]

RewriteRule ^index.php$ http://www.example.co.uk/ [R=301,L]

Redirect 301 /index.php?cat=7 http://www.example.co.uk/?cat=7
----------------------

jdMorgan

4:50 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are several things wrong with the code. Most important of which is that the code redirects from example.co.uk to www.example.co.uk, which tells search engines to use the www.example.co.uk subdomain as the canonical domain, and not the example.co.uk domain. This is the opposite of what you appear to want.

The second rule will create an 'infinite' loop if index.php is defined as the DirectoryIndex file. While it may eventually give you the result you want, there will likely be eight to ten redirects in a row every time "index.php" is requested by a client.

Your last directive won't work, because mod_alias isn't aware of query strings, and can take no action based on them. The good news is that your second rule would handle that case as-is, so this third directive isn't needed. However, I'll show a commented-out example of how you might handle the query string if you needed to change the category from 7 to 8. Note that since it is a more specific rule than that derived from your original second rule, this example rule would need to be placed ahead of the original second rule as shown in order to have the desired effect.

Modified code:


RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk [NC]
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]

# Commented-out -- example only; Change cat from 7 to 8 for "index.php" or "/" page requests
# RewriteCond %{QUERY_STRING} ^cat=7$
# RewriteRule ^(index\.php)?$ http://example.co.uk/?cat=8 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ http://example.co.uk/ [R=301,L]


Jim

wheelie34

5:17 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks very much Jim, much appreciated