Your browser does not seem to support CSS. If images appear below, please disregard them.
toggle
May
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Sponsored Links
Latest Postings
Topic Options
Rate This Topic
#50254 - 02/01/10 10:42 AM Forcing WWW (or Non-WWW) on URLs (via Mod Rewrite)
Joined: Feb 2002
Posts: 7,194
Gremelin Offline
Community Owner
Gremelin Offline

Community Owner

Joined: Feb 2002
Posts: 7,194
Portland, OR; USA
For whatever reason, you decide that you want to force one set of URLs towards your users, either ProWWW or AntiWWW. Well, with Apache there is a very simple tool to do this, it's called Mod_Rewrite and you can use a .htaccess file to do this.

There are many complicated methods to do this, over the years we've used a good set of them, that is until we found a route to just use one that's triggered for any URL (versus one for every TLD that we own).

First, your .htaccess file must tell the server that "hey, I'd like to use Mod_Rewrite" and then have a set of rules; both of these are:
Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Line 1 turns mod_rewrite on, lines 3 and 4 tell mod rewrite to look for urls missing the "www" prefix and then rewrite the URL with www attached.

Now, if you'd like to go the other route and force a non-www url on your site you can do this:
Code:
RewriteEngine on

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


This similar rule says that if the www portion of the URL is attached, to strip it away and use just the domain and the uri.

Now some users will argue that "www" is depreciated and shouldn't be used; whereas others would argue that "www" means web, and "ftp" means ftp (etc etc etc), whatever you choose (if wanting to force either) that's up to you.

From an SEO standpoint, I'd say to choose one or the other; some scripts (mainly those which use cookies) will treat both URLs as different sites, so I'd advise you go with one and stick with it.

And yes, we're in the "pro www" camp :P.


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Top
Sponsored Links
#50255 - 02/01/10 10:45 AM Re: Forcing WWW (or Non-WWW) on URLs (via Mod Rewrite) [Re: Gremelin]
Joined: Feb 2002
Posts: 7,194
Gremelin Offline
Community Owner
Gremelin Offline

Community Owner

Joined: Feb 2002
Posts: 7,194
Portland, OR; USA
In case the above pro-www doesn't work for you, one of the others we've used is:
Code:
RewriteEngine on

rewritecond %{http_host} ^([a-zA-Z0-9]+\.net) [NC]
RewriteRule (.*) http://www.%1/$1 [R=301,L]


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Top

Member Spotlight
Gremelin

Gremelin
Portland, OR; USA
Posts: 7,194
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Forums46
Topics45,730
Posts80,898
Members2,157
Most Online1,567
Apr 25th, 2010
Top Posters(All Time)
UGN Security 38,893
Gremelin 7,194
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Newest Members
Herbert_Sherbert, codemauve, Lillysdragon1984, Brewwit, boa
2157 Registered Users
Who's Online Now
1 registered members (Cyrez), 3 guests and 1 spider.
Latest News