Forum Moderators: phranque
I've found these two possibilities
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [ncrewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
One has [r=301,nc] and the other has[L,R=301]
Do they do something differently or do both accomplish the same thing.
[edited by: engine at 10:28 am (utc) on Feb. 8, 2008]
[edit reason] examplified [/edit]
the last post in this thread [webmasterworld.com] has two templates with explanatory comments for your requirements.
I went with this one from the thread you sent me to.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
[edited by: jdMorgan at 12:29 am (utc) on Feb. 9, 2008]
[edit reason] example.com [/edit]
as far as the .htaccess file
here is mine
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
[edited by: jdMorgan at 12:33 am (utc) on Feb. 9, 2008]
[edit reason] No URLs please. [/edit]
You might also want to redirect index file URLs before doing the
non-www and www stuff.
I wondered about that. How would it look if I combined it with
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I've found I can edit it easily with HTML Notepad by downloading the old one
then opening it and making changes. Love those old tools.
I had already tested it simply by noticing that if I looked for the non www version it would flip over the the www. It's good to know there about status code checkers so I can try that.
Options +FollowSymLinks
RewriteEngine on
# if index.html is requested, redirect ...index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
# if subdomain is missing, rewrite to www. subdomain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
what does the [A-Z]{3,9}\ /([^/]+/) do?
that regular expression is everything in the request up to but not including the "index.html HTTP/"
The code has that extra stuff on it so that it can work both for index files in the root of the site and for index files in folders. The folder-path is preserved in that redirect. That's a Good Thing.
Do I have the winner in the code below? I don't need to leave in the comments do I?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
probably the best way to see the results is by typing in
http://example.com/index.htm
and you should see
#1 Server Response: http://example.com/index.htm
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Sun, 10 Feb 2008 20:13:53 GMT
Location: http://www.example.com/
Connection: close
Redirect Target: http://www.example.com/#2 Server Response: http://www.example.com/
HTTP Status Code: HTTP/1.1 200 OK
Date: Sun, 10 Feb 2008 20:13:53 GMT
Connection: close
That will show you that first the index.htm is redirected to / and then the non-www is redirected to www.
so it looks like this:
example.com/index.htm -> example.com/ -> www.example.com/
It will show that the first rule redirects index.htm to www.example.com/ all in one step, no matter whether it is requested as example.com/index.htm or www.example.com/index.htm
The second rule is not invoked in this case. To test the scoend rule, request example.com/ and you should see that get redirected to www.example.com by virtue of the second rule. Or you can test the second rule by requesting any URL-path except the /index.htm path that invokes the first rule.
Jim
[edited by: jdMorgan at 8:28 pm (utc) on Feb. 10, 2008]
Actually, that's not what it will show... Thankfully.
Um. ok, a little confused here...
when the request is for index.htm, or index.php, the server is showing a 301 redirect to www.example.com/
then it queries www.example.com/ which returns a status 200 OK message.
i had mentioned using example.com/index.htm as the URL to make sure that it redirected to www.example.com/ domain without the index.htm at the end of the file path.
If that's not what the header check is supposed to show, what is it supposed to show?
non-logical way of how the redirects look graphically
Jake knows enough about my background to know the "non-logical" way would make it clear to me. ;) But the more complete info will be good for people reading this thread who have a clue.
My own highly technical way to check it was to put in
http://example.com/index.htm
and
http://www.example.com/index.htm
then watch to see if the right URL pops up at the top of my screen.
It all works great now. I just want to thank everyone. I have enough understanding of the whole thing that I won't mess it up again like I did to the one my server people put up a couple of years ago. Yet I didn't have to spend days trying to figure it out on my own and still wonder if I had messed things up again.
Thanks again
Anne
[edited by: tedster at 4:42 am (utc) on Feb. 11, 2008]
[edit reason] switch to example.com [/edit]
That will show you that first the index.htm is redirected to / and then the non-www is redirected to www.
so it looks like this:
example.com/index.htm -> example.com/ -> www.example.com/
All that shows (and should show) in the headers display above is:
example.com/index.htm --301 redirect--> www.example.com/ (200-OK)
And this is the correct and desired result, canonicalization of both the index page URL-path and the domain with a single redirect.
Jim
i would suggest the Live HTTP Headers 0.13.1 firefox add-on [addons.mozilla.org] for checking these types of header/redirect issues.