.htaccess - how to force “www.” in a generic way?
This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
.htaccess mod-rewrite
add a comment |
This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
.htaccess mod-rewrite
add a comment |
This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
.htaccess mod-rewrite
This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
.htaccess mod-rewrite
.htaccess mod-rewrite
asked Feb 6 '11 at 21:21
StackOverflowNewbieStackOverflowNewbie
13.8k81226381
13.8k81226381
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
Thanks! I would addRewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.
– Ryan Casas
Oct 7 '15 at 16:50
|
show 1 more comment
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
add a comment |
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
add a comment |
This won't work with subdomains.
domain.com correctly gets redirected to www.domain.com
but
images.domain.com gets redirected to www.images.domain.com
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*).(.*). [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
@iUseMagentoNow just increase the dots, for example:RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still.uk
and your domain issomething.co
.)
– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
add a comment |
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
add a comment |
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com
to www.example.com
excluding subdomains.
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
add a comment |
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
add a comment |
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$
https://www.sitename.com/$1 [L,R=301,NC]
add a comment |
protected by starkeen Dec 15 '16 at 16:50
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
Thanks! I would addRewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.
– Ryan Casas
Oct 7 '15 at 16:50
|
show 1 more comment
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
Thanks! I would addRewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.
– Ryan Casas
Oct 7 '15 at 16:50
|
show 1 more comment
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
edited Nov 24 '18 at 12:38
Max Vollmer
5,73651737
5,73651737
answered Feb 10 '11 at 15:13
GumboGumbo
506k90671759
506k90671759
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
Thanks! I would addRewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.
– Ryan Casas
Oct 7 '15 at 16:50
|
show 1 more comment
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
Thanks! I would addRewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.
– Ryan Casas
Oct 7 '15 at 16:50
25
25
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be:
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
– Jared Pomranky
May 14 '13 at 19:04
2
2
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...
– lol
Jul 18 '13 at 23:21
3
3
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant
– code_monk
Feb 22 '14 at 21:31
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.
– Clain Dsilva
Mar 3 '15 at 5:27
1
1
Thanks! I would add
RewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.– Ryan Casas
Oct 7 '15 at 16:50
Thanks! I would add
RewriteEngine On
as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.– Ryan Casas
Oct 7 '15 at 16:50
|
show 1 more comment
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
add a comment |
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
add a comment |
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
edited Aug 6 '18 at 0:46
Wade
2,60821938
2,60821938
answered Feb 6 '11 at 21:36
Martin DrapeauMartin Drapeau
1,2521314
1,2521314
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
add a comment |
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
Simple, and effective!
– ChaseMoskal
Apr 30 '14 at 8:44
1
1
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
Thanks, but what about https:// ?
– Lucas Bustamante
Sep 26 '17 at 19:21
add a comment |
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
add a comment |
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
add a comment |
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
answered Dec 17 '14 at 9:13
ClickForWebsClickForWebs
493
493
add a comment |
add a comment |
This won't work with subdomains.
domain.com correctly gets redirected to www.domain.com
but
images.domain.com gets redirected to www.images.domain.com
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*).(.*). [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
@iUseMagentoNow just increase the dots, for example:RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still.uk
and your domain issomething.co
.)
– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
add a comment |
This won't work with subdomains.
domain.com correctly gets redirected to www.domain.com
but
images.domain.com gets redirected to www.images.domain.com
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*).(.*). [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
@iUseMagentoNow just increase the dots, for example:RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still.uk
and your domain issomething.co
.)
– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
add a comment |
This won't work with subdomains.
domain.com correctly gets redirected to www.domain.com
but
images.domain.com gets redirected to www.images.domain.com
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*).(.*). [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This won't work with subdomains.
domain.com correctly gets redirected to www.domain.com
but
images.domain.com gets redirected to www.images.domain.com
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*).(.*). [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
edited Nov 24 '13 at 3:07
answered Nov 24 '13 at 3:02
john mccarthyjohn mccarthy
312
312
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
@iUseMagentoNow just increase the dots, for example:RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still.uk
and your domain issomething.co
.)
– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
add a comment |
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
@iUseMagentoNow just increase the dots, for example:RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still.uk
and your domain issomething.co
.)
– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
8
8
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
This won't work with certain TLDs, like .co.uk
– kieranajp
Dec 11 '13 at 11:07
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?
– TylersSN
Mar 12 '15 at 13:57
1
1
@iUseMagentoNow just increase the dots, for example:
RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still .uk
and your domain is something.co
.)– mirabilos
Dec 15 '15 at 14:17
@iUseMagentoNow just increase the dots, for example:
RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC]
(Strictly speaking, your TLD is still .uk
and your domain is something.co
.)– mirabilos
Dec 15 '15 at 14:17
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
.co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.
– Chris
Feb 4 '16 at 21:43
add a comment |
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
add a comment |
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
add a comment |
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
answered Feb 6 '11 at 21:40
RoUSRoUS
854625
854625
add a comment |
add a comment |
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com
to www.example.com
excluding subdomains.
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
add a comment |
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com
to www.example.com
excluding subdomains.
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
add a comment |
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com
to www.example.com
excluding subdomains.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com
to www.example.com
excluding subdomains.
edited Jan 25 '17 at 5:39
Albert Renshaw
9,1941368152
9,1941368152
answered Jul 1 '16 at 4:12
starkeenstarkeen
29.9k145878
29.9k145878
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
add a comment |
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
For me this seems to be sending www.example.com to www.www.example.com haha
– Albert Renshaw
Jan 25 '17 at 5:41
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
@Albert Perhaps this is because of your browser cache. Clear your cache and Retry.
– starkeen
Jan 25 '17 at 6:04
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
You are correct! I just ran in incognito (chrome) and it worked fine, thanks!
– Albert Renshaw
Jan 25 '17 at 6:37
1
1
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?
– Sacha Vorbeck
Jul 3 '17 at 9:13
1
1
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds
– Sacha Vorbeck
Jul 3 '17 at 16:36
add a comment |
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
add a comment |
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
add a comment |
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
answered Jul 12 '18 at 22:26
Rick HellewellRick Hellewell
299216
299216
add a comment |
add a comment |
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$
https://www.sitename.com/$1 [L,R=301,NC]
add a comment |
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$
https://www.sitename.com/$1 [L,R=301,NC]
add a comment |
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$
https://www.sitename.com/$1 [L,R=301,NC]
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$
https://www.sitename.com/$1 [L,R=301,NC]
answered Nov 7 '17 at 9:26
SamYahSamYah
62559
62559
add a comment |
add a comment |
protected by starkeen Dec 15 '16 at 16:50
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?