Forum Moderators: phranque

Message Too Old, No Replies

How to convert non-www to www for sub-directory level WordPress pages

         

biglearner

4:11 am on Aug 30, 2007 (gmt 0)

10+ Year Member



I did a lot of search but did not find a straight for avoiding duplicate pages for sub-directory Wordpress blog.

I just set up a website. I have a few static pages and folders at the root level, and then I want the Wordpress blog to be under the sub directory like http://www.example.com/blog/. I try to direct non-www to www for all pages including those generated by WP. My following files will give me correct results for my non "/blog/" pages.

Now at the root level my .htaccess file says:

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

At the /blog/ level I have following default .htaccess:

# 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

But when I access example.com/blog it won't add "WWW" in front; instead it accessed WP via example.com/blog/ (which is what I try to avoid due to duplicate issue). My question: what additional rewrite rule should I add into blog's htaccess file?

Please help as I desperately want to set it up right from the beginning to avoid duplication issues.

wheelie34

9:49 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



welcome to the forum biglearner

Heres what I have in a WP sites' htaccess, it works for me.

Options +FollowSymlinks
RewriteEngine on

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

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

My rewritecond & rewriterule are different than yours I see, JD should be able to say which works best, sure he'll be along soon for you.

[edited by: jdMorgan at 1:34 pm (utc) on Aug. 30, 2007]
[edit reason] example.co.uk [/edit]

jdMorgan

1:40 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have two choices:

Either copy the domain canonicalization code into /blog/.htaccess, or add the RewriteOptions inherit directive to that file. Either should work.

The difference in the two domain canonicalization code snippets is that biglearner's code will redirect requests for all domains not equal to www.example.com, whereas wheelie34's code will redirect (only) requests for the single non-canonical domain example.co.uk to the canonical www.example.com.uk domain.

Jim

[edited by: jdMorgan at 1:40 pm (utc) on Aug. 30, 2007]

biglearner

3:44 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



Thanks for your help but I still can't get it work. I could not edite my origianl post so here I re-clarify what I mean:

Suppose I have root-level pages, also I have my Wordpress folder under www.example.com/blog/.

Here is my domain .htaccess file:

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

Here is the .htaccess file under /blog/ level:

# 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

MY INTENTION: I would like it show "http://www.example.com/blog/" when I type in "example.com/blog".

With my current .htaccess files, when I type in "example.com/blog" or "example.com/blog/", it gives me result without "www", that is, "example.com/blog/";

When I type in anything else, it shows "http://www.example.com/whatever" correctly.

I tried to copy the same RewriteCond and Rule from the root level to /blog/ level file, but it gave 404 error when I type in "example.com/blog"; it gave me "http://www.example.com/" when I typed in "example.com/blog/".

What should I do? If you need look at my actual site, please sticky mail. Thanks.

[edited by: biglearner at 3:52 pm (utc) on Aug. 31, 2007]

jdMorgan

3:51 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this in /blog/.htaccess:

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

I removed a bunch of junk from the wordpress code. It's not needed.

Jim

biglearner

4:12 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



Thanks Jim. After adapting your code for /blog/ .htaccess, I am one-step closer to what I want. Here is the result:

When I type in "example.com/blog/" it shows "http://www.example.com/blog/" THAT'S GREAT!

However when I type in "example.com/blog" it does add the wanted prefix and shows the correct page but also inserts my server root path, something like this:

http://www.example.com/blog/the/root/path/www/blog

where it should show http://www.example.com/blog/

How to correct this problem?

jdMorgan

4:19 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is a serve configuration error. Do you have access to the server configuration file? If not, ask your host to fix this.

Jim

biglearner

5:29 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



I just asked my hoster Future Quest, but they insisted it is a .htaccess issue. Can anyone still help me out? I believe my scenario would be typical for anyone put WP into sub-directory instead of root folder.

jdMorgan

10:19 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have seen this problem before, but it is not a common problem. Your code does not do anything that should inject the server filepath into the URL; Server filepaths don't belong in URLs!

Unless you have other rules in this .htaccess file, in .htaccess files above this directory, or in .httpd.conf (perhaps added by cPanel), and one of those rules is incorrect, this is a server configuration problem.

It may be possible to fix this using RewriteBase /the/root/path/www/blog for the redirect rule and RewriteBase / for the internal rewrite rule, but this should not be necessary. The very fact that you had the problem you describe in your first post to this thread indicates that something's not right with this server. When the service contract expires, I'd switch to a new host...

If your host is using their own rewrites as part of their server setup, they may have forgotten to use the [PT] flag on their rules.

Jim

g1smd

6:59 pm on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whenever I get that, I use an optional (stuff/i/do/not/want)? in brackets, to remove it from the redirect like this:

RewriteCond %{HTTP_HOST} ^(.*\.)?domain\.com$ [NC]
RewriteRule ^(home/user12345/public_html/sitename)?(.*)$ http://www.domain.eu/$2 [R=301,L]

This one was redirecting all subdomains on domain.com to the virtual host on the same server at www.domain.eu too.

Adapt to suit.

jdMorgan

7:03 pm on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a good work-around, but I sure would like to get some feedback on what the exact error mechanism is... It might be helpful for others who have this same problem in the future.

Jim

rxbbx

11:35 pm on Sep 13, 2007 (gmt 0)

10+ Year Member



@jdMorgan
Can confirm this.. it gives the server root path when you use /blog/$1.

This seems to work for me..

%{REQUEST_URI} in stead of /blog/$1 .. earlier thread: [webmasterworld.com...]