Fixing Canonical URLs Problem with Wordpress URL Rewrite
I was having trouble with canonical URLs issue with this blog. When you entered http://shihengcheong.com/blog it would be redirected to
http://www.shihengcheong.com.com/blog/
You see, I’ve been used mod_rewrite to redirect all URL with www- prefix to the non-www URL i.e.
http://shihengcheong.com/
The mod-rewrite in the .htaccess at the root directory works fine.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^shihengcheong.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://shihengcheong.com/$1 [L,R=301]
</IfModule>
Last month, I installed Wordpress to the folder, /blog/, and I got stuck with the canonical URLs problem.
Here’s the problem. I guess people do not type the trailing slash when they enter the URL.
This is probably what most people will type:
shihengcheong.com/blog
So, for the past month, you’ve probably noticed that the displayed URL at your browser address bar was
http://www.shihengcheong.com/blog/
and not the shorter version without the www.
I was not very happy with this www URL. I want the search engine robots to index my page as http://shihengcheong.com/blog/ and hide away all other variations of URLs that will display the identical page.
I am no mod_rewrite and .htaccess expert. In fact, I simply copy what’s working and modify it according. The intricacies of mod_rewrite and web server programming is not my cup of tea.
Modifications to Wordpress URL Rewrite .htaccess
Finally, I stumbled upon a snippet of sample .htaccess codes for some Wordpress plugins yesterday. And I got an idea to solve my canonical URLs problem. Here’s how I resolved the trailing slash problem.
I added the following codes to the .htaccess created by Wordpress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^shihengcheong.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://shihengcheong.com/blog/$1 [L,R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>>
# END WordPress
If you are new to canonical URLs problem , you may want to read Matt Cutts SEO Advice on URL Canonicalization. And yes, Google might have fixed the problem but there are many more search engines out there which are not as smart.
See Also...
- Wordpress 2.0.7
Yesterday, less than 14 hours ago, I installed Wordpress 2.0.6 to my... - Microblogging with Wordpress and Prologue
Using the Prologue theme with Wordpress and you'll get a Twitter lookalike.... - Finally, I am Blogging on Wordpress
Wordpress ROCKS! I am totally impressed by Wordpress. It was a breeze...