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

寻求可行的301重定向方案:HTTP转HTTPS、非www转www

301 Redirect Configuration to Force HTTPS + www

Got it, let's get you sorted with the exact 301 redirect rules you need — all variants of your domain will point to https://www.domain.tld/ as required. Below are configurations for the two most common web servers:

Apache (using .htaccess or httpd.conf)

If you're running Apache, add these rules to your root .htaccess file (or your server's virtual host config):

# Enable rewrite engine
RewriteEngine On

# Redirect HTTP (both bare and www) to HTTPS + www
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]

# Redirect HTTPS bare domain to HTTPS + www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^domain.tld$ [NC]
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]
  • RewriteEngine On turns on Apache's URL rewriting module (make sure this module is enabled on your server first).
  • The first rule catches any HTTP request (whether it's http://domain.tld or http://www.domain.tld) and redirects it to the HTTPS + www version.
  • The second rule handles the case where someone accesses the HTTPS bare domain (https://domain.tld) and sends them to the www variant.
  • Don't forget to replace domain.tld with your actual domain name!

Nginx

For Nginx, add these rules to your server block configuration (usually found in /etc/nginx/sites-available/ or similar):

# Redirect HTTP bare domain to HTTPS + www
server {
    listen 80;
    server_name domain.tld;
    return 301 https://www.domain.tld$request_uri;
}

# Redirect HTTP www domain to HTTPS + www
server {
    listen 80;
    server_name www.domain.tld;
    return 301 https://www.domain.tld$request_uri;
}

# Redirect HTTPS bare domain to HTTPS + www
server {
    listen 443 ssl;
    server_name domain.tld;
    # Include your SSL certificate config here (ssl_certificate, ssl_certificate_key)
    return 301 https://www.domain.tld$request_uri;
}

# Your main HTTPS + www server block
server {
    listen 443 ssl;
    server_name www.domain.tld;
    # Add your SSL config, root directory, index files, and other site settings here
}
  • Each server block targets a specific domain variant and sends a 301 redirect to the desired https://www.domain.tld URL.
  • Make sure your SSL certificates cover both domain.tld and www.domain.tld (a wildcard cert or SAN cert works here).

Quick Test Tip

After setting up the rules, you can verify they work using curl in your terminal:

curl -I http://domain.tld
curl -I http://www.domain.tld
curl -I https://domain.tld

Each should return a 301 Moved Permanently status code pointing to https://www.domain.tld/.

内容的提问来源于stack exchange,提问作者J-ZA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:35:01