Forum Moderators: phranque

Message Too Old, No Replies

Canonical Redirect Problem

Setting up a canonical redirect through htaccess

         

Hoegaarden

5:06 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



Hi all.

I've been setting up redirects from non-www to www in htaccess using the following rules -

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

This works on the inner pages of my site, for example is i go to http://example.co.uk/page1.html i get redirected to http://www.example.co.uk/page1.html

However, the problem is with the homepage. When i go to http://example.co.uk/ i get redirected to http://www.example.co.uk/index.php

So i tried this -

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

Now when i go to http://example.co.uk/ i get redirected to http://www.example.co.uk/

Great, that's what i want but... when i go to http://example.co.uk/page1.html i also get redirected to http://www.example.co.uk/

Can any one point in the right direction please? I want it to redirect to http://www.example.co.uk/page1.html

papajay

5:43 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



try this,

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

jdMorgan

5:55 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unescaped literal perdods and a missing space before the flags in that RewriteCond. It should be:

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

The problem with seeing /index.php in your browser address bar is likely that you've got your rules in the incorrect order.

Put all of your external redirects (those with "http://www.example.com" in the substitution URL and/or [R=301] flags) first, followed by all of your internal rewrites. End each rule with a [L] flag. Within each of these two groups (redirects and rewrites), order the rules from most-specific patterns and conditions to least-specific. This avoids unexpected operation and prevents 'exposing' your internal filepaths as URLs to the client.

Jim

Hoegaarden

9:30 am on Dec 4, 2009 (gmt 0)

10+ Year Member



Many thanks for your replies.

I tried those but still getting the same. Inner pages are fine as in if i try a non www version of inner pages it'll redirect me to the www version. The home page problem still exists though, where it is redirecting me to index.php.

I'm quite new to this so i don't understand what you mean by "put all your external redirects".

I'm actually using Joomla for the website and it has it's own predefined rules for search engine friendly urls. I think this may be interferring. The rules are as follows -


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/¦\.php¦\.html¦\.htm¦\.feed¦\.pdf¦\.raw¦/[^.]*)$ [NC]
RewriteRule (.*) index.php [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

jdMorgan

4:14 pm on Dec 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Horrid Joomla code...

Your new redirect rule must be placed ahead of your Joomla rewrite code. Otherwise, it will interfere.

Please look at this thread concerning Joomla mod_rewrite code [webmasterworld.com], as I don't want to have to repeat all that detail here. The modified code in that thread will be at least twice as fast as what Joomla has provided to you...

Jim

Hoegaarden

4:54 pm on Dec 4, 2009 (gmt 0)

10+ Year Member



Many thanks. My rule is already placed before the joomla one but i'll check out that thread.
Thanks again for the help.