Forum Moderators: phranque

Message Too Old, No Replies

http://mydomain.com needs to become http://www.mydomain.com

trying to use htaccess to insert missing www. so cookies will work

         

britsky2

9:53 pm on Oct 30, 2004 (gmt 0)



I'm trying to add missing "www." if the visitor leaves it off when typing in my URL. For instance, [mydomain.com...] would become [mydomain.com...]

I found code examples in an archived thread here, but they caused an Forbidden error when I tried them.

Here's the code I put in my top-level .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule ^(.*)$ [mydomain.com...] [R=301,L]

I'm getting a 403 Forbidden Error when I make this my .htaccess file - even when it's the ONLY block of code in there.

Thanks, and God bless!
-Jeff Gannaway

nancyb

11:16 pm on Oct 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am the equivalent of a newbie at this, but try this

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*) [domain.com...] [R=permanent,L]

Note the "!" is removed as that means "not" (I think... :)

jdMorgan

1:44 am on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In this case, either block of code should work.

The most likely cause of your 500 Server Error is a missing space between "}" and "!" in the RewriteCond -- posting code on this board removes those spaces, and this leads to chronic errors with copied code.

The second-most-likely cause is that your server configuration will require you to add the line

 Options +FollowSymLinks 

ahead of your code block, in order to allow mod_rewrite to run. If this is the case, you should see a specific error message in your server error log describing this requirement.

nancyb's code will also work just fine. The difference is that your code says, "If non-blank and not desired domain, redirect to desired domain," while nancyb's code says, "If undesired domain, redirect to desired domain."

So, nancyb's code is specific to one undesired domain, while your code will rewrite any domain except the desired one. This can be useful if you have a lot of possible domains or subdomains that resolve to your server and need to be redirected.

Jim

nancyb

2:24 am on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you, Jim. Every time I think I know something about this, I discover just how little I really know. :(

This one's also bookmarked, too!

androvboy

11:24 am on Nov 2, 2004 (gmt 0)

10+ Year Member



This seems to give the exact answer:

[httpd.apache.org...]

althouh I know nothing about this stuff!