.htaccess help - adding "www"

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

.htaccess help - adding "www"

Post by tpost »

Hi everyone,

Right now, if i type "www.domain.com/blog", I get redirected to "domain.com/blog".

I'm not a regex expert, so I wouldn't know which RewriteCond and RewriteRule would work in the htaccess file to prefix the "www" to serendipidty URLs.

Thanks in advance!
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

To redirect all traffic to http://www.example.com so none of it lands at http://example.com, use this in your htaccess file:

Code: Select all

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
=Don=
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

Hi Don,

Right now all traffic from our domain already appends the "www".

Only for the s9y blog, does it not reflect this...

We're using this on the root .htaccess

Code: Select all

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Apache doesn't pay attention to the root .htaccess if there's one in the current directory.

And Serendipity puts one in its directory.

Try adding Don's lines just above the Serendipity lines in the .htaccess in the Serendipity directory.
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I wasn't paying attention.... if your serendipity install is domain.com/blog, use this in the serendipity root (ie /blog) htaccess file:

Code: Select all

RewriteEngine On
RewriteBase /blog/
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/blog/$1 [R=301,L]
=Don=
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

Don Chambers wrote:I wasn't paying attention.... if your serendipity install is domain.com/blog, use this in the serendipity root (ie /blog) htaccess file:

Code: Select all

RewriteEngine On
RewriteBase /blog/
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/blog/$1 [R=301,L]
Hi Don,

Putting in those lines in my s9y .htaccess, gives me a weird URL.

It now prefixes the "www", but also suffixes the actual physical path to the web root.

This is what it outputs:

Code: Select all

http://www.domain.com/blog//home/username/public_html/blog
Any idea what is happening?

Thanks!
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Try constraining the RewriteRule match to the beginning and end of the URL with ^ and $:

Code: Select all

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/blog/$1 [R=301,L]
Judebert
---
Website | Wishlist | PayPal
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

judebert wrote:Try constraining the RewriteRule match to the beginning and end of the URL with ^ and $:

Code: Select all

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/blog/$1 [R=301,L]
Hi judebert,

I tried your constraining fix on the RewriteRule, but I still get the same result...

Any other ideas?
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Judebert is the guy who helped me figure out the suggestions I have already provided.... however, is it possible that your root rewrite rules are impacting the suggestion for the s9y folder rules????
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

I have to admit it looks like that. I just can't tell why. It's possible it's something in the <VirtualHosts> section of the Apache config file; that's faster than .htaccess anyway, and it overrides everything.

As it says on the Apache mod_rewrite intro page, "mod_rewrite is voodo. Damned cool voodoo, but still voodoo." Often that means the only solution is to keep plugging at it until something works.

I wonder if the problem could be that we're using the HTTP_HOST variable in the rewritten URL. We know what host we want; let's use it directly:

Code: Select all

RewriteRule ^(.*)$ http://www.domain.com/blog/$1
If that's not it, the Apache rewriting examples have a recipe a lot like ours, but it uses a slash in its RewriteRule:

Code: Select all

RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
That's just the sort of magic I'd expect to work, too.
Judebert
---
Website | Wishlist | PayPal
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

Hi judebert,

A little bit of headway!

Using your first suggestion, if one typed "domain.com/blog/" with the trailing slash, it would redirect to "www.domain.com/blog/"

However, if you typed "domain.com/blog" without the trailing slash, you would get the weird suffixed, physical path as well:

Code: Select all

http://www.domain.com/blog//home/username/public_html/blog
So if there was a way to add a trailing slash to "domain.com/blog", that would solve all the problems!
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

I think the second rule does exactly that. It removes any leading slashes from the current URL, then adds a slash after blog/ to compensate. Have you had a chance to try it yet? Or is that what's still getting the local path?
Judebert
---
Website | Wishlist | PayPal
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

Hi judebert,

Using the second RewriteRule you listed:

Code: Select all

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
Outputs these results:

Typed in address -> Output address

www.domain.com/blog -> domain.com/blog/ (bad)
www.domain.com/blog/ -> www.domain.com/blog/ (good)
domain.com/blog -> www.domain.com/blog/home/username/public_html/blog (really bad!)
domain.com/blog/ -> domain.com/blog/ (bad)
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

tpost wrote: www.domain.com/blog -> domain.com/blog/ (bad)
That... shouldn't happen. That can't happen, within my understanding of the Apache .htaccess -- therefore my understanding must be incomplete.

It should've just skipped my rules and continued with whatever's below them. What's below that rewrote the URL to remove the www? It's looking for a file named 'blog', so maybe the Index or the ErrorDocument is still pointing at domain.com?
That's what I was expecting! It skips my rule and nothing changes.
tpost wrote: domain.com/blog -> www.domain.com/blog/home/username/public_html/blog (really bad!)
This I just don't understand. I'll spend some time examining the Apache mod_rewrite documentation and recipes this weekend.

WAIT! That's specifies the file "blog" in the directory / (really, /home/username/public_html/ to the filesystem). So Apache wouldn't bother to check the .htaccess in the blog/ directory; it only uses the one in the / directory! To make this work as expected, we'll need to work with BOTH .htaccess files!

The /.htaccess should contain an Index and ErrorDocument of its own, along with this rule:

Code: Select all

RewriteRule ^(.*)/blog$ http://www.domain.com/blog/ [L, QSA]
Which will match any URL ending in "/blog" (without the ending slash) and send it to your actual blog directory.

That also explains the first mismatch. The /.htaccess is being processed, not the /blog/.htaccess. It probably contains a rule omitting the www.
tpost wrote: domain.com/blog/ -> domain.com/blog/ (bad)
THIS is not at all what I expected. That URL clearly matches the .htaccess rule, and should've been rewritten with www prepended. I've still got to go check the mod_rewrite docs, it seems.
Judebert
---
Website | Wishlist | PayPal
tpost
Regular
Posts: 77
Joined: Fri Sep 21, 2007 1:45 am

Post by tpost »

tpost wrote:Hi judebert,

Using the second RewriteRule you listed:

Code: Select all

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
Outputs these results:

Typed in address -> Output address

www.domain.com/blog -> domain.com/blog/ (bad)
www.domain.com/blog/ -> www.domain.com/blog/ (good)
domain.com/blog -> www.domain.com/blog/home/username/public_html/blog (really bad!)
domain.com/blog/ -> domain.com/blog/ (bad)
Oh crap!

Just wanted to get back to you Judebert about these results.

I just used the RewriteRule used in the quote again... and I provided you with the wrong results!

This is what I actually get:

Code: Select all

domain.com/blog -> domain.com/blog
domain.com/blog/ -> domain.com/blog/
www.domain.com/blog -> domain.com/blog/
www.domain.com/blog/ -> www.domain.com/blog/
Any other ideas?
Post Reply