【问题标题】:Apache site does not load without http or https in front of itApache 站点在没有 http 或 https 的情况下无法加载
【发布时间】:2019-11-15 21:51:08
【问题描述】:

当我不包含 http:// 或 https:// 前缀时,我的网站不会加载。

例如,这些不加载: example.com , www.example.com

但这确实: https://example.comhttp://example.comhttp://www.example.comhttps://www.example.com

我使用 Apache 作为我的网络服务器,这是我的站点可用配置:

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

        <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>

我确实有一个由 certbot 生成的 SSL 证书。

关于为什么会发生这种情况的任何想法?

提前致谢

【问题讨论】:

  • 因此,在您的 http / 端口 80 的 vHost 配置中,您只需重定向到 https。为什么你那里有额外的东西?您是否启用了站点(您引用了可用的站点)。为什么你有这些 RewriteConditions 在那里?整个配置只适用于 example.com 和 www.example.com,因此无需再次检查。但是,我找不到明显的错误。你能显示 SSL / https / 端口 443 的配置吗?当您在没有 http 或 https 的情况下尝试浏览器时,您的浏览器会做什么?检查开发工具的网络选项卡并注意重定向。

标签: php apache url digital-ocean lamp


【解决方案1】:

你可以试试这个

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

    <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 %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://{SERVER_NAME}%{REQUEST_URI} [L,NE,R=301]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-02
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多