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/.
PHP-- Redirect http://domain.com/ to http://www.domain.com/
Moderator: General Moderators
Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/
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.
it can be done with php of course, but it's better handled at the host.
Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/
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:
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:
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!
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R,L]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]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
Re: PHP-- Redirect http://domain.com/ to http://www.domain.com/
This Is What I Was Looking For Thank You