You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将子域名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:

  1. 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.
  2. 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;
}
  1. 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:

  1. Find the virtual host file for test.example.hr (typically in /etc/apache2/sites-available/). Create a new file if it doesn’t exist.
  2. 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>
  1. Enable required modules if you haven’t already: a2enmod rewrite ssl
  2. Test the config with apache2ctl configtest, then restart Apache: systemctl restart apache2

Key Notes to Remember

  • SSL Certificate Coverage: Ensure your example.hr SSL certificate includes test.example.hr (a wildcard cert like *.example.hr or a SAN cert works). Without this, visitors will hit SSL errors before the redirect triggers.
  • Redirect Type: Use 301 for permanent redirects (best for SEO and long-term moves) or 302 if this is a temporary change.
  • Testing: If you’re testing locally, update your hosts file to point test.example.hr to your example.hr server’s IP so you can verify the redirect works without waiting for DNS propagation.

内容的提问来源于stack exchange,提问作者oracle02

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:18:16