Forum Moderators: phranque

Message Too Old, No Replies

.htaccess, and canonical issues

using word press blog

         

jakegotmail

11:38 pm on Dec 5, 2006 (gmt 0)

10+ Year Member



Hello,

I am very new to htaccess and need some help. basically what I am trying to do is alleviate any chance of google crawling my site with http://example.com/blog/articles-go-here

I just set up a blog located at http://www.example.com/blog/

I want to ensure that google never see's my site without "www." for cannonical issues that could become a headache down the road.

I am currently using the following snippet to keep my root directory pages pointing to the right place:

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST}!^www\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

It seems for each directory you have on your site, it needs a seperate .hta file in it. What code would be needed to make this work in the blog directory?

I am open for any suggestions if there is a better way to do this.

jdMorgan

12:04 am on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> It seems for each directory you have on your site, it needs a seperate .hta file in it.

No, not usually. What makes you draw that conclusion?

Jim

[edited by: jdMorgan at 12:05 am (utc) on Dec. 6, 2006]

jakegotmail

12:18 am on Dec 6, 2006 (gmt 0)

10+ Year Member



I think I read it somewhere, and the fact that word press drops the file in the blog directory vs. the root directory lead me to that conclusion.

Honestly after I wrote that and reread it I thought to myself that might not be the case.

jakegotmail

2:21 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



I used a auto generator to produce the code for the root directory redirects, my question still remains of how do I handle http://www.example.com/blog/ so it never points to http://example.com/blog/? Any ideas would be helpful.

jdMorgan

2:35 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tested this?

In the normal situation, the mod_rewrite code above, if located at the Web root of your site will generate a redirect from no-www to www- no matter what subdirectory is requested.

This is a powerful server configuration function, and warrants in-depth testing.

Jim

jakegotmail

3:29 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



Interesting, perhaps its a WordPress issue.

It redirects everything properly except everything in the blog directory.

----

edit:

Just went and tested a new directory. It works. SO it most def. must be a WordPress issue.

Now that I think about it. I am using the fuction of WordPress to mod rewrite the names of pages to be functional names instead of?id=12. So there is a htaccess file in the blog directory. Here is the code for it:

# 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

I am guessing the file in the blog directory is overwriting the file in the root directory.

jdMorgan

5:03 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try putting a copy of the domain redirect code *after* the word press code, inside the <IfModule> container.

Jim

jakegotmail

6:05 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



The code in the .hta file stored in the blog directory now looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /blog/index.php [L]
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST}!^www\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
</IfModule>
# END WordPress

which incidentally did not work. Any other ideas?
Thanks for the comments Jim.

jdMorgan

6:22 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I mis-typed, and even emphasized the mis-type...

This should be closer:


<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /blog/
#
# Redirect to canonical domain
RewriteCond %{HTTP_HOST} !^www\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/blog/$1 [R=301,L]
#
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# END WordPress
</IfModule>

Because of the RewriteBase, this may still need some tweaking -- no way to tell without testing.

Jim

jakegotmail

6:33 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



It works!

Thanks a lot Jim.