1

I am currently issueing the following problem and by hell, i can't solve it. The > 20 similar threads i found with google are no help.

First of all, i created a simple HTTP to HTTPS redirect, which looks like this:

server {
    listen 80;
    server_name my-domain.com;
    return 301 https://my-domain.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name my-domain.com;
    ...
}

And this works perfectly fine. If i try to open my-domain.com, i will get redirected to https:// my-domain.com. But there is still the issue with subdomains. If i open www.my-domain.com or test.my-domain.com, i would like these subdomains to redirect to my root HTTPS domain: https:// my-domain.com. Without any subdomains in front of it. The same should appear for HTTPS subdomains. I literally want to ban any subdomains (for now).

I have tried a lot of configurations, like adding *.my-domain.com to the server_name, and still this doesn't work as expected. I also tried to create a HTTPS redirect from *.my-domain.com to my normal HTTPS domain, and still i have a similar result with each configuration:

  1. All HTTP requests result in a HTTPS url containing the subdomain.
  2. All HTTPS requests never result in a HTTPS url WITHOUT a subdomain in it.

Now my Question: How can i redirect every subdomain(http and https) to a specific https URL containing NO subdomain in it? Every combination should always result in: https:// my-domain.com$request_uri

Thanks in advance

Update:

Didn't solve my issue fully, but i have something i can live with. I created a CNAME from www.my-domain.com to my-domain.com in my domain configuration. I also modified nginx and added a new server configuration which redirects from 443 www.my-domain to https:// my-domain. i also have a normal port 80 server which redirects also from www.my-domain to https:// my-domain. I modified my certificate and added www.my-domain to the list of domains and subdomains. Now every request on www.my-domain(http or https) will redirect to https://my-domain. Also my-domain(http and without www.) will redirect to https://my-domain too. This seems "okay" for my needs.

0

You could use a default_server block to match any http and https domains not explicitly defined in another server block. The https part only works properly with wild-card certificates.

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    ... ssl configuration
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    ...
}

See this document for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.