301 Redirect from HTTP to HTTPS not working

Hi

Hestia 1.7.7 on Ubuntu 20.04 running nginx

I need to 301 redirect requests from http://example.com to https://example.com - including any file/directory requests.

I have tried using the checkbox in Hestia CP with no success

I have tried creating a file: /home/xadmin/conf/web/example.com/nginx.conf_redirect_rules
with content:

# Redirect Configuration:
server {
      listen         80;
      server_name    *.example.com example.com;
      return 301 https://$host$request_uri;
}

no success

I have tried creating a template: /usr/local/hestia/data/templates/web/nginx/redirect_rule.tpl

with content:

server {

    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;

}

No success

I have restarted nginx each time, even restarted hestia… no success.

I’m not sure either the content of these files nor the location is 100% correct, but I have tried!

Could someone help please?

Thanks

I ended up editing /etc/nginx/conf.d/domains/example.com.conf and adding:

server {
    listen 80;
    server_name example.com;

    return 301 https://$host$request_uri;
}

at the end of the file.

I finally have cURL response:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 31 May 2023 15:00:11 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://example.com/

Will get overwritten during next upgrade…

1 Like

How do I prevent it getting overwritten during next upgrade?

Create a template for it:

2 Likes

I did that originally, with no effect. I’ve done it again, still no effect.

file: /usr/local/hestia/data/templates/web/new_default.tpl

Contents:

server {
    listen     80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

curl -I http://example.com
response:

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 01 Jun 2023 13:28:56 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3231
Last-Modified: Wed, 31 May 2023 09:20:17 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "64771151-c9f"
Accept-Ranges: bytes

Is the template being loaded? Do I need to do anything else to make it load?

Did you update in the domain selection → template it should work fine

there is no option for the new template in domain selection

You need create /usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.tpl

2 Likes

Thanks Eris.
I created /usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.tpl

with total content:

server {
    listen     [::]:80;
    server_name *.example.com example.com;
    return 301 https://$host$request_uri;
}

and after restarting nginx and hestia, new_default appears in the drop down list, but now I get this when I save the domain edit page:

Should I copy default.tpl and append my server block, or is simply creating a new file with that block sufficient?

You need to create both .stpl and tpl file :slight_smile:

1 Like

oh ok, so the list of files I need to create would be:

/usr/local/hestia/data/templates/web/nginx/new_default.tpl
/usr/local/hestia/data/templates/web/nginx/new_default.stpl
/usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.tpl
/usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.stpl

and the content in all those files would be:

server {
    listen     [::]:80;
    server_name *.example.com example.com;
    return 301 https://$host$request_uri;
}

is that correct?

[edit]
If I add a 301 to https in the .stpl files, won’t that cause an infinite loop?

You need for the default.stpl use default.stpl contents …

1 Like

That makes more sense, thanks :slight_smile:

so now I have:

/usr/local/hestia/data/templates/web/nginx/new_default.tpl
/usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.tpl

with contents:

server {
    listen     [::]:80;
    server_name *.example.com example.com;
    return 301 https://$host$request_uri;
}

and /usr/local/hestia/data/templates/web/nginx/new_default.stpl

which is a copy of /usr/local/hestia/data/templates/web/nginx/default.stpl

and /usr/local/hestia/data/templates/web/nginx/php-fpm/new_default.stpl

which is a copy of /usr/local/hestia/data/templates/web/nginx/php-fpm/magento.stpl
(copying default.tpl broke Magento).

I have set the domain template to new_default, restarted nginx and hestia

BUT

Still no redirect from http to https

Do you have any more ideas?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.