Redirecting http:// to http://www.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I want to redirect all from http://mysite to http://www.mysite.

I have cookies/sessions that obviously only work on the one it's set on, and want to avoid any problems people "might" get into with this.

Is this correct:

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

Are there any pitfalls with this? As I understand it, it wont break browser back functionality as they would never really have went to http://mysite

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

It looks like that will only work if they go to your home page. What if somebody linked http://example.com/insidepage.htm ? Here is what I use on my site:

########################################
# make sure we are at the right domain #
########################################

RewriteCond %{HTTP_HOST} ^www.wells-it.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^wells-it.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.wells-it.com$ [NC]
RewriteRule ^(.*)$ http://wells-it.com/$1 [R=301,L]

(I am forcing the opposite, which is www to no www, so change as necessary)

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I just can't get it to work. Whatever I try it seems to do nothing.
I can't even get a 500 error (dammit at least I'd have done something then).

I'm using your code and removing the two you have for redirecting your .net to .com, and switching the http / www around.

Like so:

RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

Edit
It seems the rewrite engine is not on by default on the server as I thought it was. Turning it on in the htaccess now gives me an infinite loop with the above code, but I resolved that now as I used the [NC,OR]. Got rid of the OR (fairly logical I guess) and now it works.

Cheers

For anyone reading and needing the working code:

#redirect all and any pages from http:// to http://www.
RewriteCond
%{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Yes, the [OR] probably caused a problem because that's an OR statement when I had other conditions. You took out the conditions and left the OR, which probably confused the server.

I did have the RewriteEngine On statement, but that was all the way at the top of my .htaccess file, and I have a ton of other junk I didn't copy.

They have: 83 posts

Joined: Apr 2009

Hello Friend PLZ ADD THIS CODE IN BETWEEN THE HEAD TAG of your index page and just change the desire redirect domain with it, when any visitor will come to your site it will redirect then to your another site.Hope you are searching for this, if it is then plz use the following patch of code in your site.
Thanks.
_________________________________________________________________________

<script>
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)

//if NS 6
if (browser_type=="Netscape"&&browser_version>=5)
window.location.replace("http://www.discusionpoint.com/forum/")
//if IE 4+
else if (browser_type=="Microsoft Internet Explorer"&&browser_version>=4)
window.location.replace("http://www.discusionpoint.com/forum/")
//if NS4+
else if (browser_type=="Netscape"&&browser_version>=4)
window.location.replace("http://www.discusionpoint.com/forum/")
//Default goto page (NOT NS 4+ and NOT IE 4+)
else
window.location="http://www.discusionpoint.com/forum/"
</script>

__________________________________________________________________________

greg's picture

He has: 1,581 posts

Joined: Nov 2005

What happens if they don't have Javascript enabled? Or they have a different browser to the above?

They have: 83 posts

Joined: Apr 2009

Don't worry I have tested it in all types of browsers and also tested it without enabling the javascript, here its working fine and there is no problem with this code its easy to understand and be sure it will work in other browsers which has not mentioned above, if you wish you can also add other name ..i just provided the alternate method to your question one can easily add this code and it redirect to desired site within half second of time....If is there any fault with my code then I am sorry.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I would like to see a working example of this javascript code that DOES work on a browser with javscript shut off.

BTW, here is how I have my things set up.

I have one domain name that is using DynDNS to get routed to my server at home, both domain1.com and www.domain1.com go there.

For domain2.com, it is set up for a very basic account at a hosting company (so that my e-mail goes there, and I can transfer backups to that account). But, I actually want the web site to be on my home server. So I create custom DNS entry with www.domain2.com being a CNAME for domain1.com (just domain2.com cannot be a CNAME, it has to be an A record to point to a static IP address, which I'm not paying an additional $40 a month for)

So now if you go to http://domain2.com it goes to the hosting account, and http://www.domain2.com goes to my own server. Here is what all I have in place to redirect people from the hosting account over to my own server. I place this in a .htaccess file in the root of the domain's directory:

RewriteEngine On
RewriteRule ^.*$ http://www.domain2.com%{REQUEST_URI} [R]

Note, the simplicity of this works as the www. and non www. are on two separate servers, so there is no need to do any checking.

IMO, here is an order to attempt to define things:

DNS records
Server Settings
Serverside Scripting redirect
Meta tag redirect
Javascript redirect

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.