View Full Version : Howto convert domain.com to www.domain.com automagically?

01-20-2003, 10:23 AM
Hi All, and welcome to my first post here :)

A simple question (with a simple answer I hope!).

If you goto http://php.net/ it will automatically change it to http://www.php.net/. I want to do this with one of my domains. I have full root access so can do anything that is required (although dropping in an htaccess would be the neatest way for me).

Anyone know how to do it?



01-20-2003, 12:13 PM
Well, usually it is the way you have your DNS and zones set up.

If you have a zone for www. then there shouldn't be a problem with using either. However to change it to www. every time, you are right, you would have to use a .htaccess file.

I have put together a quick rewrite rule that you should just stick in your .htaccess file:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

# domain.com -> www.domain.com
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !domain.com/
RewriteRule ^(.*)$ www.domain.com [L]

Like I say, I did that real quick and haven't tested it. Plus I am no guru at .htaccess.

Give it a try and see what heppens :) If it doesn't work, just remove it :)

Oh and keep me posted :D

::] krycek [::

01-20-2003, 12:33 PM
I already have both domain.com and www.domain.com pointing to the same place so both work. I am thinking about PR and google :)

OK I ended up using this.

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L]

Not exactly what you said, but as soon as I saw what you did say I realised my mistake before... I hadn't got the FollowSymlinks in there!!!



01-20-2003, 12:35 PM
Glad it works :p

BTW - did mine work? Did you try it? Or just jump straight in with yours? I am just wondering if I got it right, lol :)

::] krycek [::

01-20-2003, 12:53 PM
Nope, I just saw my mistake and went straight ahead with mine. Tell you what, I've got to nip out but when I get back I'll stick yours in to see if it works and let you know the outcome :)

Thanks again


01-20-2003, 01:47 PM
Originally posted by krycek BTW - did mine work?

Yep :)

01-20-2003, 02:57 PM
Cool! :cool:

I haven't had much experience doing .htaccess files etc. so it is good to know that I did it right :)

Is there any particular difference in function between the two...?

I am just curious because you took a different approach and I am trying to compare them and learn the difference etc.

::] krycek [::

01-20-2003, 03:13 PM
Don't ask me lol.. looks like regexp hell. I just used that from what someone gave me but didn't work until I realised that I must have symlinks auto off in my httpd.conf. Oh well.

The 301s are return codes, but that's about it. :)