homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Canonical URL - WITH Wordpress blog in Subdirectory

 10:17 am on Mar 11, 2010 (gmt 0)

Hi there,

I have a question about canonical URL.

I have been using the following code for around 2 years without any problem. Until recently, after installing a Wordpress blog in a subfolder, I have encountered 1 problem. The code was :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^myurl\.com [NC]
RewriteRule ^(.*)$ http:// www.myurl.com/$1 [R=301,L]

**Note that I put a space in front of www because the forum will automatically wrap it into a clickable URL if I don't do so.

I tried to install my wordpress blog in this directory named specialoffers , so the full directory would be http:// www.myurl.com/specialoffers/ , if I wanted to visit my blog.

Now the problem is, I tried to access my blog by typing http:// myurl.com/specialoffers/ , which was without the www. It keeps on redirecting and never able to successfully load my blog up. However, if I type http:// www.myurl.com/specialoffers/ , it can then be loaded up without any problem.

So I tried to delete the rewrite engine code from my .htaccess, it works for the non-www and the www version. My blog shows up.

So can anyone please help me with this problem? I would like my entire site (including the Wordpress blog directory to be loaded up with WWW). Is there any other codes that I can use in this case?

I've tried this in Google Chrome, Firefox, and also Internet Explorer, all of them were unable to load up the page as mentioned.

I have posted the image of the problem for each of the 3 website explorers and Google Chrome has a notice saying TOO MANY REDIRECTS.




Thanks in advance for any help given.



 7:39 pm on Mar 11, 2010 (gmt 0)

Wordpress has its own internal canonical handling I believe. Check those settings.

One fix would be to exclude the /blog/ folder from your canonicalisation rule.

Additionally, the canonicalisation rule could do with some improvements:

# Redirect non-canonical non-blank hostname to www canonical
RewriteCond $1 !^blog/
RewriteCond %{HTTP_HOST} [b]!^(www\.[/b]example\.com[b])?$[/b]
RewriteRule (.*) http://www.example.com/info/$1 [R=301,L]


 2:18 am on Mar 12, 2010 (gmt 0)

Hi there g1smd,

Thanks for the information given, but it's not working for me. The directory which I've installed wordpress, will still shows up as non-www after entering your code.

I wish there are some codes where I can put it into my .htaccess file to please my non wordpress directory and the wordpress directory altogether .... :(


 7:31 am on Mar 12, 2010 (gmt 0)

The code above excluded the blog folder from being 'fixed' within .htaccess. Now look at the canonical settings within Wordpress to fix Wordpress itself.

By excluding the blog folder from your rule in .htaccess you have stopped the infinite loop. That was the only purpose of that change.


 8:03 am on Mar 12, 2010 (gmt 0)

Oh ok, thanks.

I tried Googling about Canonical URL for Wordpress, they said by default, it should be fixed, for newer version. Mine is newest version, but it was still having the non-www problem. Which section in webmasterworld should I head to?


 10:00 pm on Mar 14, 2010 (gmt 0)

Search WebmasterWorld for the keywords "wordpress canonical RewriteCond HTTP_HOST" to narrow things down a bit. (See the "search" link at the top of this page)

Look for the more recent (Feb/Mar 2010) threads where fixes for some of the WP mod_rewrite code's more egregious inefficiencies are discussed.



 1:55 am on Mar 15, 2010 (gmt 0)

Thanks for pointing me to that direction, Jim. Appreciate that :)


 3:23 am on Mar 15, 2010 (gmt 0)

Hi Jim, I tried to search using the forum's search function, for the phrase which you've suggested. There are many questions being asked but I can't find one that is specifically identical to my issue.

I am a dummy when it comes to webhosting stuff, and the apache stuff, and also this kind of coding stuff. I can't understand why they use such coding line in such situation. This is really a headache situation for me.


 2:23 pm on Mar 16, 2010 (gmt 0)

Our purpose here is to educate, and not to attempt to provide a "free coding service" or "help desk." We have too few contributors to provide such services, and doing so would mean leaving you with code that you do not understand and cannot maintain. Which means that if you have a problem, you'd be right back here asking for more support and increasing the workload of the limited number of volunteers we do have. This is simply not a sustainable activity.

For this reason, we encourage you to study the documentation and to ask specific questions, which are always welcome.

As g1smd pointed out, the likely problem is that your WP installation is set up with the wrong canonical domain -- Your mod_rewrite code redirects example.com to www.example.com, but WP is set up to redirect www.example.com to example.com, thus creating an 'infinite' redirection loop.

So the simple fix, assuming that we've correctly identified the problem, would be to go into WP's control panel or set-up menu and change the canonical URL from example.com to www.example.com.



 3:42 pm on Mar 16, 2010 (gmt 0)


This time I get what you both meant.

Excuse me for me poor understanding on English, as this is not my first language. Now everything seems to make sense !

Thanks for pointing that out, for what g1smd had mentioned to me, as you've mentioned that in here --> As g1smd pointed out, the likely problem is that your WP installation is set up with the wrong canonical domain -- Your mod_rewrite code redirects example.com to www.example.com, but WP is set up to redirect www.example.com to example.com, thus creating an 'infinite' redirection loop.

Now I know where to head for, and what to look for, thanks for the clarification Jim! You saved me a lot of headache.


 4:49 pm on Mar 19, 2010 (gmt 0)

Thanks Jim and g1smd.

After Googling for a while, I found out some codes to be used and they worked pretty well with my site at this moment.

Just one thing though, the loading time of my site's pages seems to be a bit slower than usual.

I have placed a canonical url code the .htaccess file in my root (public_html), and another canonical url code in the .htaccess file in my wordpress subdirectory (example.com/specialoffers/) . Can you please take a look at those codes, were they being used correctly in my case?

Thanks a lot for any help given in advanced :)

Canonical url code in root (public_html) :

# Remove index.php from root URL
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

# Permanently redirect from non-www domain to www domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Canonical url code in Wordpress subdirectory (/specialoffers/):

# Comprehensive URL Canonicalization for WordPress in Subdirectory
RedirectMatch permanent index.php/(.*) http://www.example.com/specialoffers/$1

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]

RewriteBase /specialoffers/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html|php)$ http://www.example.com/specialoffers/$1 [R=301,L]

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /specialoffers/index.php [L]


 5:08 pm on Mar 19, 2010 (gmt 0)

Or should I take the $ sign off the back of .com for both of the lines at :

RewriteCond %{HTTP_HOST} ^example\.com$ [NC] ?


 7:16 pm on Mar 19, 2010 (gmt 0)

That line should be:

RewriteCond %{HTTP_HOST} [b]![/b]^[b]([/b]www\.example\.com[b])?[/b]$

Redirect non-blank host header that does not exactly match "www.example.com".

This will correctly redirect any request with appended port number or trailing period, and all non-www requests.

The reason for "non-blank" matching requirement? HTTP/1.0 requests do not send a host header.

In your second example, your rules are in the wrong order. The non-www to www redirect must be listed last otherwise you get a redirection chain. Likewise the rule with -f and -d checks is very inefficient. A recent thread introduced much better replacement code for that rule.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved