Welcome New User! ( Create Account | Sign In )
Question

Submitted 353 days ago...

theDoctor

theDoctor

Beginner (14)

Inserting www to a my URL if www. is missing using the .htaccess file

If someone types http://mysite.com I want the .htaccess file to add a www. to the string, so http://www.mysite.com

What is the htaccess code to do this?

 
 
 
Answers
 

Answer 1 / 6

Submitted 353 days ago...

guido

guido

Authority (411)

Not sure you want to do it in .htaccess, but maybe you could. In any event, sounds like you are using Apache and if you put the below in httpd.conf, edited accordingly it will look for someone coming in with whatever value is in ServerName and redirecting to whatever you have in the redirect link. Some of the other data in here is used in the event of problems and could probably be dumped. There is probably a simpler way to do this too if you want to look.

<VirtualHost *:80>
ServerAdmin tech_support@domain.com
DocumentRoot "e:/apache_data/apache_content/XXX"
ServerName mysite.com
ErrorLog "e:/apache_data/apache_logs/XXX"
CustomLog "e:/apache_data/apache_logs/XXX" combined
Redirect / http://www.mysite.com/
</VirtualHost>

 

Answer 2 / 6

Submitted 353 days ago...

Ruchele

Ruchele

Brain (3,118)

You would need to give my more info for an accurate code, an example would be

RewriteCond %{REQUEST_URI} !^/users/sites/whatever
RewriteCond %{HTTP_HOST} ^whatever\.mysite\.com$ [NC]
RewriteRule ^(.*)$ /users/sites/whatever/$1 [L]

 

Answer 3 / 6

Submitted 353 days ago...

Ruchele

Ruchele

Brain (3,118)

Sorry, forgot the WWW in that question.

<Directory /www/htdocs/example>
AddType text/example .exm
</Directory>

 

Answer 4 / 6

Submitted 353 days ago...

theDoctor

theDoctor

Beginner (14)

This did it. I'm just a script kitty. Not my code I found it on the web.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.paysimple\.com$ [NC]

 

Answer 5 / 6

Submitted 353 days ago...

Ruchele

Ruchele

Brain (3,118)

That works!! Nice job.

 

Awarded Answer (What’s This?)

Answer 6 / 6

Submitted 353 days ago...

sidelko

sidelko

Brain (3,031)

Yea, that looks about right. If you were to do it for webanswers.com it would look like this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^webanswers\.com
RewriteRule ^(.*)$ http://www.webanswers.com/$1 [R=permanent,L]

Here is a blog post about this very subject if you way to read more.
http://www.chris-hooley.com/2007/05/18/canonicalize-yo-shizzle-with-ht access-every-time/

 

This Question was awarded 353 days ago therefore you can no longer post an Answer. However you may post a comment below.

 
Comments
 

Comment 1 / 1

Submitted 177 days ago...

Fakery

Fakery

Brain (2,476)

It is all about web stuff, i know like next to nothing about.

that's why other people do it and not me

 
 

Add A Comment

Email Subscriptions
Author adds clarification
All new responses

Related Questions
 
Congratulations!
sidelko

sidelko

Brain (3,031)

sidelko answer was awarded and will earn ongoing royalties from this thread.

How This Works?