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

Apache Rewrite配置求助:域名及子路径无法跳转至HTTPS带WWW地址

Fixing Apache Redirect to Preserve Request Paths

Hey there! It sounds like your current redirect setup only handles the root domain and fails to carry over specific paths (like /hello) to the target URL. Let’s fix this using Apache’s mod_rewrite to ensure every request—whether it’s the root or a nested path—gets redirected correctly to the HTTPS www version with the full URI intact.

Step 1: Enable mod_rewrite (if not already active)

First, make sure the rewrite module is turned on. On Debian/Ubuntu-based systems, run these commands:

sudo a2enmod rewrite
sudo systemctl restart apache2

Step 2: Update Your Virtual Host Configuration

Replace your existing relevant config with this snippet. This handles both non-www → www and HTTP → HTTPS redirects while preserving the full request path:

<VirtualHost *:80>
    ServerName www.123abc.com
    ServerAlias 123abc.com

    # Turn on rewrite engine
    RewriteEngine On

    # Redirect all requests to HTTPS www version, keeping the full path
    RewriteCond %{HTTP_HOST} !^www\.123abc\.com$ [OR]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.123abc.com$1 [R=301,L]

    # Keep your existing DocumentRoot line here
    DocumentRoot "/mnt/..."
</VirtualHost>

# Don't forget your HTTPS virtual host for the target domain
<VirtualHost *:443>
    ServerName www.123abc.com
    DocumentRoot "/mnt/..."

    # Add your SSL certificate configuration here
    SSLEngine On
    SSLCertificateFile /path/to/your/certificate.pem
    SSLCertificateKeyFile /path/to/your/private-key.pem
    # ... any other SSL settings you need ...
</VirtualHost>

What Each Part Does

  • RewriteCond %{HTTP_HOST} !^www\.123abc\.com$ [OR]: Checks if the request is coming to the non-www version of the domain.
  • RewriteCond %{HTTPS} off: Checks if the request is over unencrypted HTTP.
  • RewriteRule ^(.*)$ https://www.123abc.com$1 [R=301,L]: Captures the entire request path ((.*)) and appends it to the target URL ($1 is the captured path). R=301 is a permanent redirect (good for SEO), and L tells Apache to stop processing further rules after this one.

This setup will correctly handle all these cases:

  • http://123abc.com → https://www.123abc.com
  • http://www.123abc.com/hello → https://www.123abc.com/hello
  • https://123abc.com/hello/world → https://www.123abc.com/hello/world

After updating the config, restart Apache to apply changes:

sudo systemctl restart apache2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:07:44