如何将子域名test.example.hr重定向至https://example.co?
Hey there! Let's walk through how to set up that redirect from test.example.hr to https://example.co—since the subdomain lives on your example.hr server, we’ll tweak the web server configuration there. Below are step-by-step guides for the two most common web servers:
Nginx Setup
If you’re using Nginx on the example.hr server:
- Locate the server block config for
test.example.hr(usually in/etc/nginx/sites-available/or/etc/nginx/conf.d/). If there’s no existing block for the subdomain, create a new one. - Replace or add this configuration to handle both HTTP and HTTPS traffic:
# Redirect HTTP traffic to HTTPS first server { listen 80; server_name test.example.hr; return 301 https://test.example.hr$request_uri; } # Redirect HTTPS traffic to example.co server { listen 443 ssl; server_name test.example.hr; # Use your existing SSL cert for example.hr (make sure it covers test.example.hr) ssl_certificate /path/to/your/example.hr.crt; ssl_certificate_key /path/to/your/example.hr.key; # Permanent redirect to the target domain (preserves the request path if needed) return 301 https://example.co$request_uri; }
- Test the config for errors with
nginx -t, then reload Nginx to apply changes:systemctl reload nginx
Apache Setup
For Apache users on the example.hr server:
- Find the virtual host file for
test.example.hr(typically in/etc/apache2/sites-available/). Create a new file if it doesn’t exist. - Add these virtual host blocks to handle HTTP and HTTPS:
# Redirect HTTP traffic directly to example.co <VirtualHost *:80> ServerName test.example.hr Redirect permanent / https://example.co/ </VirtualHost> # Redirect HTTPS traffic to example.co <VirtualHost *:443> ServerName test.example.hr # Enable SSL and use your example.hr certificate SSLEngine on SSLCertificateFile /path/to/your/example.hr.crt SSLCertificateKeyFile /path/to/your/example.hr.key Redirect permanent / https://example.co/ </VirtualHost>
- Enable required modules if you haven’t already:
a2enmod rewrite ssl - Test the config with
apache2ctl configtest, then restart Apache:systemctl restart apache2
Key Notes to Remember
- SSL Certificate Coverage: Ensure your
example.hrSSL certificate includestest.example.hr(a wildcard cert like*.example.hror a SAN cert works). Without this, visitors will hit SSL errors before the redirect triggers. - Redirect Type: Use
301for permanent redirects (best for SEO and long-term moves) or302if this is a temporary change. - Testing: If you’re testing locally, update your hosts file to point
test.example.hrto yourexample.hrserver’s IP so you can verify the redirect works without waiting for DNS propagation.
内容的提问来源于stack exchange,提问作者oracle02
相关产品推荐
相关产品推荐

