将https非www重定向到www

时间:2019-07-09 18:48:01

标签: apache .htaccess

我要重定向:

  

https://example.com => https://www.example.com [不起作用]

     

http://example.com => https://www.example.com [不起作用]

     

www.example.com => https://www.example.com [有效]

     

example.com => https://www.example.com [有效]

.htaccess

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

虚拟主机

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/test/public_html
    ErrorLog /var/www/test/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost _default_:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/test/public_html
    ErrorLog /var/www/test/error.log
    SSLEngine on
    SSLCertificateKeyFile /etc/apache2/ssl/test.key
    SSLCertificateFile /etc/apache2/ssl/test.crt
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

我使用此.htaccess配置:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]