【问题标题】:non www to www apache2 Letscrypt certbot非 www 到 www apache2 Letsencrypt certbot
【发布时间】:2021-08-17 16:15:02
【问题描述】:

我想将我的非 www 重定向到 www 。 SSL 工作正常,两个 url 都与 ssl 工作正常。 https://example.com https://www.example.com 两者都有效,但我想将https://example.com 重定向到https://www.example.com

我正在使用 AWS ec2 中的 Lamp-server,并将 certbot 用于 ssl。

我的 apache 配置。

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我尝试了许多在线教程,但没有任何帮助,在此先感谢您的帮助或支持。

【问题讨论】:

  • "我的 apache 配置。" - 您只发布了服务器配置的部分,即。端口 80 (HTTP) 的 vHost。如您的问题所述,要从https://example.com(HTTPS)重定向到https://www.example.com,那么您需要为端口443(HTTPS)找到您的vHost,即。 &lt;VirtualHost *:443&gt;.

标签: apache apache2 lets-encrypt certbot


【解决方案1】:

尝试(注意:如果不删除以# 开头的 cmets,Apache 可能会抛出错误):

# turns on the rewrite engine
RewriteEngine On
# checks if domain is not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# redirects to www.example.com
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# the above is enough for 443 VirtualHost

# checks if https is not on
RewriteCond %{HTTPS} !on
# redirects to https on the same domain
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

那么,你的完整配置:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

将以下内容添加到&lt;VirtualHost *:443&gt; 配置中:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

  • "RewriteCond %{HTTPS} !on" - 无需检查端口 80(即 HTTP)的 vHost 中的 HTTPS 是否为 on。尽管通过扩展,这实际上并没有回答问题,因为它没有重定向https://example.com(注意HTTPS)-因为它位于端口80的vHost中。(所有vHost:80需要做的就是重定向一切 到 HTTPS+规范主机,即Redirect 301 / https://www.example.com/。)
  • Apache 不支持 line-end cmets,因此您的第一个代码块(带有 line-end cmets)如果按原样使用,实际上会触发 500 错误,因为第 4 个指令(即。@ 987654331@) - “错误的标志分隔符”。
  • (此问题已出现在近距离投票审核队列中。)
  • @MrWhite "(这个问题已经出现在近距离投票审核队列中。)",这是否意味着这个问题将被删除?
  • @MrWhite 这是一个旧答案。在发布答案时,我没有当前的知识。我会相应地编辑答案
猜你喜欢
  • 2020-03-11
  • 2018-12-12
  • 2021-09-13
  • 2023-03-03
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多