1

I am facing an issue for my .htaccess redirect to www from non www I like to redirect any tld domain like .com or .co.uk etc. I am using the following solution, from this link [Redirect non-www to www in .htaccess

# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

but it only redirect tld with .com/.net etc but can't figure out how to change this so that it can grab any tld like .co.uk or .co.in etc.

EDIT: I have sub-domain there as well so using following rule not gonna work.

RewriteCond %{HTTP_HOST} !^www\.

Thanks in advance.

2 Answers2

0

Updated answer for subdomain:

# rule for forcing www on main domain
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# rule for removing www on sub domains
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Andrii Pryimak
  • 797
  • 2
  • 10
  • 33
  • Hi, Thanks for your suggestion. I forgot to say that I have tried this solution as well before but I have sub-domain there. So this solution makes my sub-domain no accessible. N.B. I am gonna edit my question so that others don't misunderstand me. – Tapan Kumer Das Nov 22 '17 at 08:26
0

You are pretty close. You can use:

RewriteCond %{HTTP_HOST} ^[^.]+\.(?:com|co\.uk)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

If you have Apache 2.4+ then you can use %{REQUEST_SCHEME} variable:

RewriteCond %{HTTP_HOST} ^[^.]+\.(?:com|co\.uk)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hi buddy! Thanks for your help, I am sorry that I forgot to mention that I have sud-domain for those domains as well, so this rule makes those sub-domain inaccessible. Can you help with that, please? Thanks. – Tapan Kumer Das Nov 22 '17 at 08:31
  • What are all top level you need to support? I guess `.com` and `.co.uk`, anything else? – anubhava Nov 22 '17 at 09:47
  • I appreciate your effort. Let me explain a bit more about my scenario. I built an application where a user can register them and add their domain so that they will get the login page when they go to their domain. Now I would like to force www with all the domain. In this case I am not sure what would be the TLD. So I like to grab any TLD either with 3 letter or 2 and 3 letter or any combination. I hope you got my point. Thanks in advance. – Tapan Kumer Das Nov 22 '17 at 17:47
  • No, users cannot just create any domain with any TLD. At the most users can create a subdomain of an existing domain that you own. e.g. if you have `example.com` registered then your users may be able to create `user1.example.com`, `user2.example.com` etc but they cannot create `user1.anything.co.jp`, hope you understand this. – anubhava Nov 22 '17 at 18:29
  • I am not sure why you say so. But the fact is I built the system and user can create their account and create a page. After that they add their domain in admin panel and add an A record to their domain to my server IP so that when they go to their domain it shows the page created by my application. It all works now and I use the following rule, RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ But it only grab TLD with single dot (ie, .com/.net/.org etc) but if they use other TLD like .co.uk, .co.in, .co.jp etc then it wont work. So what I need to change this rule something that can grap all TLD. – Tapan Kumer Das Nov 23 '17 at 04:12
  • Ok but there is no regex pattern that I am aware of to differentiate *all* possible domains from all the subdomains. – anubhava Nov 23 '17 at 04:29
  • Ohh thats unfortunate :( Anyway thanks for your help :) – Tapan Kumer Das Nov 23 '17 at 13:05