DIY SEO: Canonical URL’s, htaccess and Joomla
OK, I had to scour the internet to find bits and pieces to this question. What lines do you need to add to your .htaccess file to get canonical URL’s to resolve properly for a Joomla 1.5 site? Well after an exhaustive search of numerous blogs and chat forums, I managed to add 6 “rewrites” to my .htaccess file that have solved the problem for my client’s site without causing any issues in the administrator back-end or otherwise (knock on wood, I’m still testing it out). Before I get to the additions, I should probably explain the whole canonical issue for anyone that is just learning. Basically, Google counts the following URL’s as different pages even though they are essentially the same page as far as you and your site are concerned:
- https://www.yardstickservices.com
- https://www.yardstickservices.com/
- https://www.yardstickservices.com/index.php
- https://yardstickservices.com
- https://yardstickservices.com/
- https://yardstickservices.com/index.php
And this applies to all the pages of your site which is why this is so important. If you want to get maximum pagerank, you need to get this sorted. OK, so here’s the code that I added to my root .htaccess file with an explanation to follow:
RewriteEngine On
RewriteBase /
# prevents people from accessing anything with phpMyAdmin
RewriteRule ^/phpMyAdmin.*$ https://www.yardstickservices.com
# force www
RewriteCond %{HTTP_HOST} ^yardstickservices\.com$ [NC]
RewriteRule ^(.*)$ https://www.yardstickservices.com/$1 [R=301,L]
# remove index.php within the URL
RedirectMatch permanent index.php/(.*) https://www.yardstickservices.com/$1
# remove index.php at the end of the URL and change to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
# Remove index.php from root URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html) [NC]
RewriteRule ^index\.php$ https://www.yardstickservices.com/ [R=301,L]
# Add a trailing slash to all URL's
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
For everyone's reference, I've pasted in suggestions made by g1smd (below) as his explanation trumps mine.
The six rules reduce to only four, and there are opportunities for other optimisations:
# prevents people from accessing anything with phpMyAdmin
# (pick one of two, second one is preferred)
RewriteRule phpMyAdmin http://www.example.com/ [R=301,L]
RewriteRule phpMyAdmin - [F]
# Add a trailing slash to all root *extensionless* URLs (but I would advise to NOT do that)
RewriteCond %{REQUEST_URI} /[^/.]+$
RewriteRule ^([^/.]+)$ http://www.example.com/$1/ [R=301,L]
# Remove index.php or index.htm/html from URL requests
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule ^([^/]+/)*index\.(html?|php)$ http://www.example.com/$1 [R=301,L]
# force canonical www if request is for non-www or has port number etc
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]- RewriteBase / is only needed if you site is on cloud hosting like Mosso/Rackspace so if you don’t know, ask your hosting provider.
- The second condition prevents any access from hackers trying to access any URL containing phpMyAdmin. I redirected this to my homepage but you may want to redirect it to some other page.
- The rest are explained by the commented out line beginning with the #
Now, this may not work for all sites depending on the extensions you have installed and the URL’s that they output so take this with a grain of salt and make sure you backup your .htaccess file (and for that matter, your entire site) before you start messing around because a mistake here can really bung things up. Otherwise, good luck and here’s to a few more notches on the old pagerank-o-meter.
