3

I build a website using NodeJs and defined the homepage in package.json to be "https://www.example.com". After npm run build I uploaded files to public folder and created htaccess file with the following code:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]
</IfModule>

Once I type http or https://example.com I am being redirected to https://www.example.com, but the site can`t be reached. How do I make it reachable by keeping the redirections from http and non-www URLs working?

I have tried deleting RewriteConds and RewriteRules responsible for redirections in .htaccess, thus making it possible to access the website using https://exapmple.com, but this is not the result I am looking for. I was also trying many different .htaccess code variations like

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC] 
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

But this brought me to the same results. It seems like I am missing something.

Diogen_Sin
  • 53
  • 5
  • Hi, welcome to SO! Please include your htaccess as code instead of an image. – Camilo Mar 11 '23 at 12:18
  • This is searchable. Your second try that you said doesn't work is from a question as an issue too. Here is the solution to that answer: https://stackoverflow.com/a/1270281/8382028 – ViaTech Mar 11 '23 at 12:23
  • 1
    Does this answer your question? [Generic htaccess redirect www to non-www](https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www) – Camilo Mar 11 '23 at 12:44
  • 1
    This doesn’t have anything to do with .htaccess. It would seem your site is simply not accessible via the www subdomain. If `www.example.com` is not even resolving then attempting to redirect is not going to help. Have you configured the DNS? Is Apache configure to accept requests to this hostname? – MrWhite Mar 11 '23 at 12:48
  • @Camilo I was working once again on the answer provided by ViaTech, but the problem is still present. I have now achieved to be redirected from http and non-www urls to https and www, but the site is still unreachable. I am now trying to work with DNS and Apache configurations as MrWhite mentioned. If you have any good sources that can lead me through the process, that would be great! I am quite a newbie in all this. Thanks for all of you! – Diogen_Sin Mar 11 '23 at 13:11
  • I have just renewed the DNS Records. I believe the issue may have been there as the CNAME www included some weird extensions to my domain. It will take up to 24 hours for changes to take effect, so I will let you know once I have the solution. – Diogen_Sin Mar 11 '23 at 13:38

1 Answers1

2

"The site can't be reached"

If the site can't be reached then the www subdomain (ie. www.example.com hostname) does not resolve. If the hostname does not resolve then the request does not even reach your server. If you can't request www.example.com directly then attempting to implement a non-www to www redirect in .htaccess is not going to help.

You need to:

  1. Configure a DNS CNAME record that points the www subdomain to the domain apex. This tells user-agents the IP address of where to send requests for the www subdomain.

  2. Configure the server to accept requests to the www subdomain. For example, on Apache in the vHost container for your site:

    ServerAlias www.example.com
    
  3. The SSL/TLS security certificate also needs to be configured to cover the www subdomain.

  4. Implement a redirect in the server config (or .htaccess) to redirect requests to the canonical hostname. ie. https://www.example.com/ (HTTPS + www). Exactly how you implement the HTTP to HTTPS part of redirect can be dependent on your server config and how the SSL cert is managed. Also, whether you have plans to implement HSTS in the future (in which case you would need to redirect from HTTP to HTTPS on the same hostname first).

    If you are not implementing HSTS then can do the following:

    # Redirect non-www to www (+HTTPS)
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Redirect remaining HTTP to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    If, however, you are implementing HSTS then you need to reverse these two rules.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thans! I just renewed the DNS CNAMEs and then reinstalled the SSL security certificates and it worked. I didn`t had anything to do with .htaccess indeed. – Diogen_Sin Mar 13 '23 at 18:04
  • @Diogen_Sin You're welcome. " I didn`t had anything to do with .htaccess" - although the directives you posted in the question are _incorrect_, since they don't redirect HTTP to HTTPS. You would need to change the rules to something like what I've included in my answer. – MrWhite Mar 13 '23 at 19:30