Back to Top

Setting Up 301 Redirects from www to Non-www...

I recently got a site launched that had a tonne of incoming links all pointing to static HTML pages on the old site.  I had to redirect as many as possible to the new equavalents on the new site running in Joomla!.  Not normally too much of a problem, except this site had an extra complication...

The main site URL had altered from http://www.domainname.com to http://domainname.com - the reason being was that the new site had a security certificate tied up to non-www, so the whole site needed to run non-www.  Again, not normally a problem, I've set-up rewrite rules for this sort of thing and it normally works just fine.

This time, however, I had a real problem.  I setup all of my 301 redirects as I normally would:

redirect 301 /page1.html http://domainname.com/content/view/13/72/
redirect 301 /page2.html http://domainname.com/content/view/14/77/
redirect 301 /page3.html http://domainname.com/content/view/14/89/

...and so on.  This worked as expected. Woo!

After my big block of redirects, I added the rewrite rules to push www users to the non-www version of the site:

Rewritecond %{https} = off
RewriteCond %{HTTP_HOST} !^domainname\.com$
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

Rewritecond %{https} = on
RewriteCond %{HTTP_HOST} !^domainname\.com$
RewriteRule ^(.*)$ https://domainname.com/$1 [R=301,L]

(I don't normally have the https condition in there but needed it for this case).  Again, this worked as expected, making sure all users were seamlessly forced to use non-www when browsing the site.  Looking good!

And then I hit the problem.

If I browsed to an old page URL, using (for example) http://www.domainname.com/page1.html - which had a redirect assigned to it - instead of ending up at the new page equivalent, the www rewrite would kick in and rewrite the whole URL to http://domainname.com/index.php.  This pretty much rendered all of my redirects useless, since almost all incoming links would be referencing the www version of the site.  I found that if I went to http://domainname.com/page1.html, the page would redirect correctly. Hmm.

So I set about trying to learn what all that code actually meant, up until now my servers had been happy with my copy & pasting without too many hassles...but this time I had to dig even deeper and see if I could work out what was happening.

I tried all sorts of combinations, and even more Google searches trying to find an answer, but nothing I tried would work properly.

Then I came across a post on a forum that gave some different examples to what I'd seen before, and I set about experimenting again...I was annoyed really that the answer was almost obvious, especially after doing so much research.  This was my first attempt, which almost worked:

#Rewritecond %{https} = off
#RewriteCond %{HTTP_HOST} !^domainname\.com$
#RewriteRule ^(.*)\.html$ http://domainname.com/$1 [R=301]

#Rewritecond %{https} = off
#RewriteCond %{HTTP_HOST} !^domainname\.com$
#RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

It was *supposed* to rewrite the URL in a certain way but only if it had "html" in the address...then skip to the global rewrite if that didn't match up; however it didn't really make much difference and didn't solve the problem (note the "[R=301]" on the first rule, the missing ",L" tells it to keep processing if no match, or so I understood it).

And so, after experimenting with my new findings I found a solution that did actually work exactly as I wanted it to: to process redirects from old www pages to the new, non-www equavalents:

Rewritecond %{https} = off
RewriteCond %{HTTP_HOST} !^domainname\.com$
RewriteRule ^(.*)$ http://domainname.com%{REQUEST_URI} [R=301,L]

...and there you have it, almost stupidly simply, right?

I hope that saves some people the hunting around that I had to do, at the very least.  And, Happy Valentines Day :)