Any random subdomain redirects to www

Redirect Random Subdomains to www

  1. Obtain an Alibaba Cloud AccessKey (using Alibaba Cloud as an example; apply for your own key).
AccessKey ID: LTAI5tQCfDcVcghn5
AccessKey Secret: J8ZayXB44MIWa1Mfnj
  1. Install acme.sh
cd ~
curl https://get.acme.sh | sh
  1. Switch the default CA to Let’s Encrypt, then reissue the certificate:
# Switch default CA to Let's Encrypt
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
# Issue a wildcard certificate (replace discourse.cc with your domain)
~/.acme.sh/acme.sh --issue --dns dns_ali -d discourse.cc -d '*.discourse.cc'
  1. Install the certificate into Discourse’s SSL directory.
    Copy the certificate to Discourse’s SSL directory

Execute as root (replace discourse.cc with your domain):

cp /home/admin/.acme.sh/discourse.cc_ecc/discourse.cc.key /var/discourse/shared/standalone/ssl/discourse.cc.key
cp /home/admin/.acme.sh/discourse.cc_ecc/fullchain.cer /var/discourse/shared/standalone/ssl/discourse.cc.cer
chmod 644 /var/discourse/shared/standalone/ssl/discourse.cc.*

Verify that the files exist:

ls -l /var/discourse/shared/standalone/ssl/

You should see discourse.cc.cer and discourse.cc.key.

  1. Modify the Discourse configuration and rebuild.
DISCOURSE_HOSTNAME: 'www.discourse.cc'
DISCOURSE_HOSTNAME_ALIASES: 'discourse.cc'
DISCOURSE_DISABLE_LETSENCRYPT: 'true'
  1. Configure Nginx redirect rules (if not already configured).
server {
    listen 80;
    server_name ~^(?!www\.)(.+)\.discourse\.cc$;
    return 301 https://www.discourse.cc$request_uri;
}
server {
    listen 443 ssl;
    server_name ~^(?!www\.)(.+)\.discourse\.cc$;
    ssl_certificate ...;
    ssl_certificate_key ...;
    return 301 https://www.discourse.cc$request_uri;
}
  1. Rebuild the container.
cd /var/discourse
sudo ./launcher rebuild app

Hi, is this not the same as @顶级域名重定向到www?

They are different. The top-level domain only allows discourse.com to redirect to www.discourse.com. For random subdomains, any random prefix.discourse.com can redirect to www.discourse.com—for example, xxxh.discourse.com, shhiaj.discourse.com, and so on, all can redirect to www.discourse.com.

לייק 1