【问题标题】:.htaccess not working while accessed via SSL/HTTPS in Apache2.htaccess 在 Apache2 中通过 SSL/HTTPS 访问时不起作用
【发布时间】:2016-08-04 03:38:13
【问题描述】:

我的 .htaccess 配置有问题。我的网络服务器是 Apache2,我的网站是用 PHP 编码的。但是,我在使用 .htaccess 时遇到了一些问题。

当我通过非 SSL (http://mywebsite.dev) 访问它时,我的 .htaccess 可以正常工作。我使用 .htaccess 进行 URL 重写并通过自定义模板处理错误。但是,当我通过 SSL (https://mywebsite.dev) 访问它时,索引页面就可以工作了。但是,当我访问https://mywebsite.dev/page/about 时,它显示 404 Not Found(显示默认的 404 Not Found 页面,而不是我的自定义页面。这意味着我的 .htaccess 代码没有加载)。

对于我的 URL 结构,它是 http://mywebsite.dev/?load=page&page=about。如果我访问http://mywebsite.dev/page/about,它就可以工作。但是,不是通过 SSL。什么是问题?顺便说一句,我的 /etc/apache2/sites-available/website.conf 代码是:

<VirtualHost *:80>                                                                          
        ServerName mydomain.com                                                              
        ServerAdmin webmaster@localhost                                                     
        DocumentRoot /var/www/html/website/                                                 

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

        # LogLevel info ssl:warn                                                             

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

        # enabled or disabled at a global level, it is possible to                          
        # For most configuration files from conf-available/, which are                      
        # enabled or disabled at a global level, it is possible to                          
        # include a line for only one particular virtual host. For example the              
        # following line enables the CGI configuration for this host only                   
        # after it has been globally disabled with "a2disconf".                             
        #Include conf-available/serve-cgi-bin.conf 

</VirtualHost>                                                                              

而且,我的 .htaccess 代码是:

RewriteEngine On                                                                      
RewriteRule ^page/([A-Za-z0-9-]+)/?$ index.php?load=page&page=$1 [NC]                 
ErrorDocument 404 /public/404.html                                                   

我正在使用 Laragon 在 Windows 中开发它。生产环境是:Ubuntu 16、PHP 7 和 Apache2。谢谢。

抱歉语法错误,或者你不明白我的意思。非常感谢您阅读我的问题:)

【问题讨论】:

    标签: php apache .htaccess mod-rewrite ssl


    【解决方案1】:

    &lt;VirtualHost *:80&gt; 行告诉 apache,此配置仅适用于所有接口 (*) 上的流量,但仅适用于端口 80 (:80) 上的流量。 AllowOverride All 未应用于 https(端口 443),因为它不匹配。

    要解决此问题,您需要另一个虚拟主机 &lt;VirtualHost *:443&gt;。您可以复制两个虚拟主机中的内容或使用包含,请参阅此server fault answer。

    【讨论】:

    • 你的意思是,我需要创建具有不同端口的新虚拟主机,以通过 https 处理请求,我错了吗? CMIIW。感谢您的回复。
    • 对,你需要两个虚拟主机来处理http和https(服务器故障问题正是如此)。如果您可以使用 https 访问您的服务器,那么您在某处已经有一个 SSLEngine on(和其他 SSLCertificateFile),您可能需要调整/重用它。
    猜你喜欢
    • 2016-06-14
    • 2012-12-31
    • 2016-10-16
    • 1970-01-01
    • 2014-05-12
    • 2013-07-15
    • 1970-01-01
    • 2012-07-16
    • 2013-09-08
    相关资源
    最近更新 更多