.htaccess help - adding "www"
.htaccess help - adding "www"
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!
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:
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=
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
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]-
Don Chambers
- Regular
- Posts: 3657
- Joined: Mon Feb 13, 2006 2:40 am
- Location: Chicago, IL, USA
- Contact:
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=
Hi Don,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]
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/blogThanks!
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,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]
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:
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:
If that's not it, the Apache rewriting examples have a recipe a lot like ours, but it uses a slash in its RewriteRule:
That's just the sort of magic I'd expect to work, too.
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/$1Code: Select all
RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
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:
So if there was a way to add a trailing slash to "domain.com/blog", that would solve all the problems!
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/blogHi judebert,
Using the second RewriteRule you listed:
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)
Using the second RewriteRule you listed:
Code: Select all
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
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)
That... shouldn't happen. That can't happen, within my understanding of the Apache .htaccess -- therefore my understanding must be incomplete.tpost wrote: www.domain.com/blog -> domain.com/blog/ (bad)
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: www.domain.com/blog/ -> www.domain.com/blog/ (good)
This I just don't understand. I'll spend some time examining the Apache mod_rewrite documentation and recipes this weekend.tpost wrote: domain.com/blog -> www.domain.com/blog/home/username/public_html/blog (really bad!)
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]
That also explains the first mismatch. The /.htaccess is being processed, not the /blog/.htaccess. It probably contains a rule omitting the www.
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.tpost wrote: domain.com/blog/ -> domain.com/blog/ (bad)
Oh crap!tpost wrote:Hi judebert,
Using the second RewriteRule you listed:
Outputs these results:Code: Select all
RewriteCond %{HTTP_HOST} !^www [NC] RewriteRule ^/(.*)$ http://www.domain.com/blog/$1
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)
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/