PHP-- Redirect http://domain.com/ to http://www.domain.com/

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

PHP-- Redirect http://domain.com/ to http://www.domain.com/

Post by Quazel »

Ok,
What I am trying to do is, if someone types in http://yourdomain.com/ I want it to redirect to http://www.yourdomain.com/.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/

Post by JakeJ »

That sort of thing should be handled at the DNS level. Check with tech support with your hosting company about how to manage that.

it can be done with php of course, but it's better handled at the host.
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/

Post by Quazel »

Thanks
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/

Post by Luke »

If, for some reason, you can't deal with it at the DNS level, you can at least deal with it at the server level. PHP is not the greatest tool for something like this (as the previous poster mentioned). If you are on Apache and you have mod_rewrite, you can add an .htaccess file and put this in it:

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R,L]
Line 1 - turns on the mod_rewrite engine in apache.
Line 2 - basically says "if the host name entered was anything BUT http://www.example.com, apply the next line". [NC] means it is Not Case sensitive
Line 3 - basically says "rewrite any URL to http://www.example.com/whatever_url_was_entered" [R,L] means that it is a Redirect (R) and that it if the rule matches, don't continue matching with any of the other rewrite rules that may be in the file. It is the Last rule. (L).

This will work as long as you don't have other subdomains that you need to work. If you do have other subdomains you will have to do something like this instead:

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|accounts|webmail).example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R,L]
In this one, basically it is saying that all domains but http://www.example.com, accounts.example.com, and webmail.example.com should be redirected to http://www.example.com.

Now you may be wondering, "why bother putting the www in there? It would be OK to redirect http://www.example.com to http://www.example.com. What will that hurt?" Well... it will send the server into a never-ending loop. So that's why :) Hope this helps!
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/

Post by Quazel »

This Is What I Was Looking For Thank You
Post Reply