UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, The GoNix Initiative, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
September
Su M Tu W Th F Sa
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
Sponsored Links
Latest Postings
Doom 3
by Cyrez
09/11/14 08:58 PM
The History Thread...
by Cyrez
09/11/14 08:56 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666
08/22/14 09:21 AM
Latest Reviews
Topic Options
Rate This Topic
#50254 - 02/01/10 02:42 AM Forcing WWW (or Non-WWW) on URLs (via Mod Rewrite)
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7192
Loc: 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, Elite Web Gamers & VNC Web Design Owner

Top
Sponsored Links
      
#50255 - 02/01/10 02:45 AM Re: Forcing WWW (or Non-WWW) on URLs (via Mod Rewrite) [Re: Gremelin]
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7192
Loc: 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, Elite Web Gamers & VNC Web Design Owner

Top

Moderator:  §intå×, Gremelin 
Featured Member
Registered: 03/02/02
Posts: 136
Forum Stats
2148 Members
46 Forums
34339 Topics
69508 Posts

Max Online: 1567 @ 04/25/10 02:20 AM
Top Posters
UGN Security 27501
Gremelin 7192
§intå× 3255
SilentRage 1273
Ice 1146
pergesu 1136
Infinite 1041
jonconley 955
Girlie 908
unreal 860
Newest Members
Gecko666, defghi795767, Devo60, ali, lavos
2147 Registered Users
Who's Online
0 registered (), 292 Guests and 268 Spiders online.
Key: Admin, Global Mod, Mod
Latest News


Donate
  Get Firefox!
Get FireFox!