Apache HTTP 到 HTTPS 重定向不起作用

Apache HTTP 到 HTTPS 重定向不起作用

我正在尝试设置默认的 HTTP 到 HTTPS 重定向。不知何故,两者mod_rewriteRedirect不起作用。我是否忽略了某些显而易见的东西?

 Listen 80
 Listen 443

<VirtualHost *:80>
         ServerName www.example.com
         ServerAlias example.com
         Redirect ^/$ https://www.example.com
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com
    SSLEngine on
    SSLCertificateFile "/etc/ssl/certs/example.com.pem"
    SSLCertificateKeyFile "/etc/ssl/private/example.com.key"

    <Directory "/web/www.example.com">
            allow from all
            Options -Indexes
    </Directory>
    #php_admin_flag safe_mode Off
    php_admin_value open_basedir /tmp/:/web/www.example.com
    DocumentRoot /web/www.example.com
    </VirtualHost>

答案1

下面是我以前使用以下命令编写的配置示例mod_rewrite

<VirtualHost *:80>
  ServerName  www.foo.com
  ServerAlias foo.com   
  RewriteEngine on
  RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>


<VirtualHost *:443>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin [email protected]
  ServerName  www.foo.com
  ServerAlias foo.com

  SSLEngine on
  SSLProtocol all
  SSLCertificateFile /etc/apache2/ssl/www.foo.com/foo_com.crt
  SSLCertificateKeyFile /etc/apache2/ssl/www.foo.com/foo_com.key
  SSLCertificateChainFile /etc/apache2/ssl/www.foo.com/intermediate.crt

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/foo.com/public_html
</VirtualHost>

相关内容