• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

mod rewrite regarding www. in web url

GeorgeB

Chairman/CEO TMCG
NLC
Just asking out of curiosity, as I have been noticing this on a few sites.

Is there some mod rewrite that is used to always make the www. in a web url display?

I have noticed it on sites like this, Facebook, and some others.

Regardless if I type the www. or not.

I have like 4 or 5 domains on my hosting account, and can access them either with or without www. to it, but not sure if the sites that I have been seeing, have some code that for some reason always make it visible?
 
Yes, it's done with mod_rewrite.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301]
 
Yes, it's done with mod_rewrite.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301]

Did you pull this from a website, or just something you learned along the way?

I ask, because I must have checked 6 or 7 sites for mod rewritting (as I did expect that is what was doing it), and I didnt see the code.

EDIT: I just tried this for the heck of it, and it returned a "500 internal server error"

Edit: It worked..stupid copy and paste.

So, is there a particular reason why sites use this?
 
Last edited:
You shouldn't do that with MOD rewrite but in your .htacces like I did on my website.
Code:
# IF REWRITE ENGINE ISNT ON ALREADY, ADD THIS
RewriteEngine On
# FORCE WWW ON URL
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
I did this because then Google only has to index 1 URL instead of 4.
www.yourdomain.com
yourdomain.com
www.yourdomain.com/index.php
yourdomain.com/index.php
 
You shouldn't do that with MOD rewrite but in your .htacces like I did on my website.
Code:
# IF REWRITE ENGINE ISNT ON ALREADY, ADD THIS
RewriteEngine On
# FORCE WWW ON URL
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
I did this because then Google only has to index 1 URL instead of 4.
www.yourdomain.com
yourdomain.com
www.yourdomain.com/index.php
yourdomain.com/index.php

Well, I don't know about you, but I place all mod rewrites in the appropriate .htaccess files.

I have never placed it anywhere else?
 
Sanderk...Yes, you take the code I posted and put it into the .htacess file. That's normally where you use mod_rewrite.

George, I pulled that code from a client's .htaccess file I created some time ago. I'm sure it can be found on the web as well.

As far as the use, the client's website was having an issue with his SSL cert. and needed the www to be forced. Aside from that, I can't really think of any good reason to do it. Some people just like the clean appearance. Facebook, for example, forces the www. Don't really think there is any rhyme or reason for them doing that.
 
Sanderk...Yes, you take the code I posted and put it into the .htacess file. That's normally where you use mod_rewrite.

George, I pulled that code from a client's .htaccess file I created some time ago. I'm sure it can be found on the web as well.

As far as the use, the client's website was having an issue with his SSL cert. and needed the www to be forced. Aside from that, I can't really think of any good reason to do it. Some people just like the clean appearance. Facebook, for example, forces the www. Don't really think there is any rhyme or reason for them doing that.

Yeah, me neither, but for the heck of it, I went ahead and added it. Mainly have seen it on bigger type websites, but I guess they want to ensure the site shows up regardless.
 
Back
Top