Welcome to WebmasterWorld Guest from 54.211.11.57

Forum Moderators: Ocean10000 & incrediBILL & phranque

Redirect all website names to this

     
8:56 pm on Feb 5, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


What would be the code for the .htaccess file so that http://www.example.com http:example.com and example.com all redirect to www.example.com please.

[edited by: not2easy at 10:24 pm (utc) on Feb 5, 2017]
[edit reason] Readability: Please use example.com [/edit]

10:40 pm on Feb 5, 2017 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3114
votes: 122


There's a good discussion in the Apache forum Library: [webmasterworld.com...] that covers best practices.
11:47 pm on Feb 5, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13543
votes: 404


http:example.com

Did you really type this, or was something damaged in editing? All the redirects in the world won't do any good if the request doesn't reach the server in the first place. Differences such as "http://example.com" vs. "example.com" (typed into the browser's address bar) don't have anything to do with your site. It's up to the browser to figure out what the user meant.
1:23 am on Feb 6, 2017 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3114
votes: 122


That was left as posted, only the domain name was edited.
7:40 am on Feb 6, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Thanks n2e.
11:03 am on Feb 6, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10761
votes: 45


http://www.example.com http:example.com and example.com all redirect to www.example.com

as lucy24 implied, the browser would have to fix up http:example.com and example.com before a request could be sent to your server, so the issues you should address are hostname and schema (protocol) canonicalization.

does your server accept https: requests?
is www.example.com the canonical hostname for all requests on that server?
are there any existing Redirect or RewriteRule directives in your configuration file or .htaccess?
11:08 am on Feb 6, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Thanks phranque, just trying to ensure that *whatever* is put before the sitename, it would still redirect to www.sitename.com
11:25 am on Feb 6, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Nov 13, 2016
posts: 348
votes: 49


In that case you must consider the catch-all *. (asterisk followed by a dot).
11:48 am on Feb 6, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10761
votes: 45


Thanks phranque, just trying to ensure that *whatever* is put before the sitename, it would still redirect to www.sitename.com

that's why i asked the questions.
2:07 pm on Feb 6, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


That's why I clarified.
2:12 pm on Feb 6, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Thanks Dimitri, I will look at that.
7:51 pm on Feb 6, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13543
votes: 404


*whatever* is put before the sitename, it would still redirect to www.sitename.com

This will depend on your DNS setup and/or your host. If you've got "wild card" subdomains enabled even though you're not actually using them, an ordinary domain-name canonicalization redirect will take care of it. If the site doesn't allow wild-card subdomains, then wrong requests such as "ww.example.com" simply won't reach your server.

In all cases, make sure the domain-name redirect is expressed as a negative: “If the request is for anything other than* this exact preferred form, then redirect to the exact preferred form.”


* It used to be “exactly suchandsuch or exactly nothing”. So the significant part of the RewriteCond would look like
!^(blahblah)?$
with parentheses and a question mark, together meaning "this part is optional". But in this day and age, I seriously doubt that any human browser--or any legitimate robot--doesn't send the “Host:” header.
7:18 am on Feb 7, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Thanks Dimitri.
10:23 am on Feb 8, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10761
votes: 45


That's why I clarified.

did i miss the answers?
11:15 am on Feb 8, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Thanks Lucy, I'll get onto the hosts.
11:42 am on Feb 8, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Okay, I asked the hosts:
Does the server have "wild card" subdomains enabled ?

They replied:
No but you can enable it by adding * record in the site dns zone
5:06 pm on Feb 8, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13543
votes: 404


If you do not, in fact, use subdomains, there is absolutely no reason to allow them. No point in cluttering up your logs with requests for "ww.example.com" when you could let the user's browser handle it. (At one time, before they jumped on the default-to-search-engine* bandwagon, "ww.example.com" was one of Firefox's explicit suggestions when you typed something in your address bar and they couldn't find it.)


* So far I have tried and failed to find the Prefs setting that overrides this behavior, because it's almost as annoying as sites that redirect to the home page instead of serving a 404. It's one reason I stuck with Camino so long :(
5:52 pm on Feb 8, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


Hi Lucy, I don't use subdomains. I am not very experienced with understanding the vagaries of .htaccess, what would would be the code please, so that *whatever** was before the website name, it would still end up as www.example.com ?
12:54 am on Feb 9, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13543
votes: 404


*whatever** was before the website name, it would still end up as www.example.com

If you do not use subdomains, and you do not have wildcard subdomains enabled, this is a non-question. If someone requests

ww.example.com
or
xyz.example.com
or
anyoldrandomgarbage.example.com

they will not reach your site (where, ahem, "example.com" means your site). So you do not have to do anything. All you need is the ordinary domain-name-canonicalization redirect, which has been posted in this subforum approximately 85,000 times so I'm sure you have it in place already.
9:47 am on Feb 9, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


This was so that if http was at the beginning, it would still go to the (ahem) www. part, but don't worry, I'll get it.
6:01 pm on Feb 9, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13543
votes: 404


if http was at the beginning

If you're talking about redirecting http to https for a secure site, that can be wrapped up in domain-name-canonicalization--the same two lines that handle with/without "www" redirects. (Fun fact: Technically, www.example.com is a subdomain of example.com, so the two could serve entirely different content. But in practice, they're generally treated as synonymous.)

Now, if you're talking about someone typing into their browser's address bar
http://www.example.com
VS. only
www.example.com
(which is what the original post kinda sounds like you were asking), that's handled by the browser before the request ever reaches your site.
9:18 pm on Feb 9, 2017 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 377
votes: 0


No-one mentioned redirecting to https - I'll come back to this another time I think. :-(
3:41 am on Feb 10, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10761
votes: 45


No-one mentioned redirecting to https


<cough>
does your server accept https: requests?
</cough>
4:54 am on Feb 10, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:7218
votes: 454


Look at what users type in (forget the serps, that's a link) and go from there. Will they www or not? that's the question. :)

Most modern browsers deal with that from the get go. (The user side)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members