Forum Moderators: phranque
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.
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]
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]
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]
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
Jim
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?
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
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.
This seems to work for me..
%{REQUEST_URI} in stead of /blog/$1 .. earlier thread: [webmasterworld.com...]