.htaccess help - adding "www"

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Looking at it, we still need two .htaccess files.

Any URL without a slash on the end will attempt to look in its parent directory for a file. So for these two URLs:

Code: Select all

domain.com/blog -> domain.com/blog
www.domain.com/blog -> domain.com/blog/
The webserver is looking for a file named "blog" in the document root directory. We need to have a .htaccess in there that looks like this:

Code: Select all

RewriteBase /
RewriteCond %{HTTP_HOST}   !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.domain.com/$1 [L,R]
RewriteRule ^blog$        blog/ [R] 
These lines were taken directly from the Apache Rewriting Guide. Note that there may be a problem with the Alias or ServerName directives in the hosting service's config file, as noted on the FAQ.

The first line establishes a base directory. The next three redirect anything not starting with www to your fully qualified domain. And the last one adds the trailing slash.

In re-examining the documentation, I found that I was wrong: .htaccess files from higher-level directories are applied to the current directory. However, the lowest-level .htaccess overrules any higher-level .htaccess files. So, by putting that .htaccess in the document root directory, it will be applied to the blog/ directory as well. Therefore, we don't need (and don't want!) any www rewriting code in the /blog/.htaccess.

In that way, the /.htaccess takes care of these cases, too:

Code: Select all

domain.com/blog/ -> domain.com/blog/
www.domain.com/blog/ -> www.domain.com/blog/ 
You probably already have a /.htaccess. Note that any Options lines in the /blog/.htaccess completely override any Options lines in the /.htaccess. When you remove the www rewriting lines from the /blog/.htaccess, be certain to copy any Options that are important from /.htaccess.

You can also remove any slash-adding code from the /blog/.htaccess. All that should be necessary in there is the Serendipity redirects.
Judebert
---
Website | Wishlist | PayPal
Post Reply