大网数据广告活动
服务器运维问答

Issues with https to https redirect in Apache

· 52CE 我爱测

I want the following redirects to happen: http://www.example.org -> https://example.org [working] https://www.example.org -> https://example.org [not working] As you can see when using "https" it's not redirecting to https://exam…

问题原文

原帖链接 · Finch · CC BY-SA 3.0

I want the following redirects to happen:

http://www.example.org -> https://example.org [working]

https://www.example.org -> https://example.org [not working]

As you can see when using "https" it's not redirecting to https://example.org but just stays at https://www.example.org

My www.conf (for www.example.org):

<VirtualHost *:80>
    ServerName www.example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www

    ErrorLog ${APACHE_LOG_DIR}/www_error.log
    CustomLog ${APACHE_LOG_DIR}/www_access.log combined

</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www

    ErrorLog ${APACHE_LOG_DIR}/www_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/www_ssl_access.log combined

    Include /etc/letsencrypt/certbot.conf
</VirtualHost>

My main.conf (for example.org):

<VirtualHost *:80>
    ServerName example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/main_error.log
    CustomLog ${APACHE_LOG_DIR}/main_access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/main_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/main_ssl_access.log combined

    Include /etc/letsencrypt/certbot.conf
</VirtualHost>

<Directory /var/www/html>
    Options -Indexes
</Directory>

已采纳解答

原帖链接 · Nic3500 · CC BY-SA 3.0

I would do this (not a complete config, just showing the concept):

<VirtualHost *:80>
    ServerName example.org
    ServerAlias www.example.org

    Redirect Permanent / https://example.org
</VirtualHost>

<VirtualHost *:443>
    ServerName example.org
    ServerAlias www.example.org

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^example.org
    RewriteRule (.*) https://example.org$1 [R=301,L]
</VirtualHost>

The idea here is one VirtualHost per port, not per domain. www.DOMAIN and DOMAIN is almost the same. So any request that comes on port 80, it redirects to https on port 443. Any request that comes to port 443 will redirect to "itself", removing the www if needed.

Combining the VirtualHost via ServerAlias is clean and simplifies your configuration.

You need to add certificates, logs, ... as I said, this is not production ready!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。