Hi, Guest ~ Login or Register

Ensuring www. is in url

Posted in Website Programming » PHP - Thursday 3rd January 2008 at 8:23PM

Alex
Member

User Avatar

Joined July 2007
Posts: 31

How could i make sure that if the user types in:

http://website.com

Instead of what i want:

http://www.website.com or just www.website.com.
______________

And i only want it to redirect if the www. hasn't been entered.


Example:

http://google.co.uk

Click the link towards google, notice how is has a script that will redirect you to www.google.co.uk



Thank you!

__________________________________
Aim2Break Records

 

Replies (8)

Replied - Thursday 3rd January 2008 at 8:42PM [Post Link]

Alec
Super Moderator

User Avatar

Joined April 2007
Posts: 124

Create a notepad file, call it htaccess.txt.

In it, place:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^sitename.com [nc]
rewriterule ^(.*)$ http://www.sitename.com/$1 [r=301,nc]


Place it in your websites root directory, and change it from "htaccess.txt" to ".htaccess". Upload it to your server. Job done.

__________________________________
My Personal Portfolio Site

 
Replied - Thursday 3rd January 2008 at 9:28PM [Post Link]

Alex
Member

User Avatar

Joined July 2007
Posts: 31

Thanks SO much :D

I never thought of using htaccess, genius.

__________________________________
Aim2Break Records

 
Replied - Friday 4th January 2008 at 12:33AM [Post Link]

Will
Administrator

User Avatar

Joined October 2005
Posts: 128

Be advised that forcing www onto a URL without it is stupid.

1. It redirects the user. (+time)
2. It means they're browsing from a subdomain, which means one extra request. (+time; +processing)

Just because Google does it doesn't mean it's right.

__________________________________
Will Morgan
Freelance Web Developer
Next feature: How to fit 25 hours into a day!

 
Replied - Friday 4th January 2008 at 9:43AM [Post Link]

Alec
Super Moderator

User Avatar

Joined April 2007
Posts: 124

Not true.

For Google, www.avengex.com and avengex.com represent two separate URLs, but for you and me it could be the same thing. It is so because you can actually have two different websites on those URLs (one www and another non-www) — it rarely happens though. When Google finds two URLs seemingly having the same/duplicate content, it indexes the one it considers the most apt.

This way it can easily miss the page you actually want it to find. It can bec aused easily by say an affiliate linking to http://avengex.com/forums/view/74/ instead of http://www.avengex.com/forums/view/74/.

The duplicate content problem can significantly harm your search engine rankings. Fortunately, canonical redirection (explained above) can solve this problem.

__________________________________
My Personal Portfolio Site

 
Sponsored Link
Replied - Friday 4th January 2008 at 11:38AM [Post Link]

Alex
Member

User Avatar

Joined July 2007
Posts: 31

My SSL thingy doesn't support domain.com just www.domain.com.

^ Thats why.

__________________________________
Aim2Break Records

 
Replied - Friday 4th January 2008 at 5:10PM [Post Link]

adam2z
Member

User Avatar

Joined October 2005
Posts: 113

[QUOTE] Alec said (4th January @ 9:43am):

blah blah

and the seo industry harms my search results. fuck them.

http://no-www.org/

__________________________________

 
Replied - Saturday 5th January 2008 at 12:32AM [Post Link]

Alec
Super Moderator

User Avatar

Joined April 2007
Posts: 124

Yes. But untill that is universal, it is simply ignorant to ignore it.

Besides, you can rewrite everything to remove the www. thus being part of your little clan, and adhearing to my point.

__________________________________
My Personal Portfolio Site

 
Replied - Wednesday 23rd April 2008 at 12:49AM [Post Link]

fedekun
Member

User Avatar

Joined April 2008
Posts: 5

here its a crappy way of doing it with PHP

$abs_path = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(preg_match("/www/i", $abs_path))
{
  $abs_path = str_replace('www.', '', $abs_path);
  header("Location: http://$abs_path");
}

__________________________________
~ Watashi wa, Eru da yo ne~