≡ Menu

Fixing Canonical URLs Problem with WordPress URL Rewrite

Cannon - Photo by Thad ZajdowiczI 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.

{ 2 comments… add one }

  • nxx.nyc 2 Nov 2008, 2:08 am

    Hi,

    Newbie here. Two questions about your “Modifications to Wordpress URL Rewrite .htaccess” above.

    1. Where can I find the .htaccess file on my server? You stated that it is located on the root level, but I’m not able to see it.

    2. Let’s say I copy your modification above, how do I save it as .htaccess file. Vista doesn’t allow me to save it as .htaccess

    Thanks so much for any help.

    NXX, NYC

  • Shi Hengcheong 2 Nov 2008, 11:45 am

    It’s not root level. My mistake. You will find it in the /public_html/ or /wwwrooot/.

    You can modify the existing .htaccess file by first downloading it to your desktop computer.

    I don’t use Vista so can’t help you there.

    Try using the notepad to create a text file name htaccess.txt. Once you uploaded to the web server using FTP, you can rename it to .htaccess. You may have to change the setting of your FTP client to display hidden files.

Leave a Comment