Forum Moderators: phranque

Message Too Old, No Replies

Redirect all website names to this

         

Dexie

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

10+ Year Member



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]

not2easy

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



There's a good discussion in the Apache forum Library: [webmasterworld.com...] that covers best practices.

lucy24

11:47 pm on Feb 5, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

not2easy

1:23 am on Feb 6, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That was left as posted, only the domain name was edited.

Dexie

7:40 am on Feb 6, 2017 (gmt 0)

10+ Year Member



Thanks n2e.

phranque

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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?

Dexie

11:08 am on Feb 6, 2017 (gmt 0)

10+ Year Member



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

Dimitri

11:25 am on Feb 6, 2017 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



In that case you must consider the catch-all *. (asterisk followed by a dot).

phranque

11:48 am on Feb 6, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

Dexie

2:07 pm on Feb 6, 2017 (gmt 0)

10+ Year Member



That's why I clarified.

Dexie

2:12 pm on Feb 6, 2017 (gmt 0)

10+ Year Member



Thanks Dimitri, I will look at that.

lucy24

7:51 pm on Feb 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*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.

Dexie

7:18 am on Feb 7, 2017 (gmt 0)

10+ Year Member



Thanks Dimitri.

phranque

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That's why I clarified.

did i miss the answers?

Dexie

11:15 am on Feb 8, 2017 (gmt 0)

10+ Year Member



Thanks Lucy, I'll get onto the hosts.

Dexie

11:42 am on Feb 8, 2017 (gmt 0)

10+ Year Member



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

lucy24

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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 :(

Dexie

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

10+ Year Member



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 ?

lucy24

12:54 am on Feb 9, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*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.

Dexie

9:47 am on Feb 9, 2017 (gmt 0)

10+ Year Member



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.

lucy24

6:01 pm on Feb 9, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

Dexie

9:18 pm on Feb 9, 2017 (gmt 0)

10+ Year Member



No-one mentioned redirecting to https - I'll come back to this another time I think. :-(

phranque

3:41 am on Feb 10, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



No-one mentioned redirecting to https


<cough>
does your server accept https: requests?
</cough>

tangor

4:54 am on Feb 10, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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)