我实际上按照这个例子操作过,结果成功了 :)
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
Redirect permanent / https://mysite.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
接着执行:
/etc/init.d/httpd restart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
使用mod_rewrite将Apache重定向HTTP到HTTPS
或者
sudo a2enmod rewrite
sudo a2enmod ssl
编辑文件
/etc/apache2/sites-available/000-default.conf
内容应为:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile <path to your certificate file>
SSLCertificateKeyFile <path to your private key file>
# Rest of your site configuration
# ...
</VirtualHost>
sudo service apache2 restart
如果你倾向于使用mod_rewrite的话:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same
location but using HTTPS.
# I.e., http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context
R=301
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
SSLEngine on
...
</VirtualHost>
<Location />
,因为那里不需要引号。(没有测试过带引号的情况。) - Tinohttps://%{HTTP_HOST}%{REQUEST_URI}
。 - bfontaineHTTP_HOST
没有被扩展,那么很可能是因为服务器没有看到请求中的Host:
头。 - smac89SERVER_NAME
必须在服务器上手动配置,但在处理多个虚拟主机时,这似乎是一个可靠的替代方案。 - smac89RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
DocumentRoot
并添加以下内容:Redirect permanent / https://[your-domain]/
服务器版本:Apache/2.4.29(Ubuntu)
在网上和Apache官方文档中进行了长时间的搜索后,唯一有效的解决方案来自于/usr/share/doc/apache2/README.Debian.gz
To enable SSL, type (as user root):
a2ensite default-ssl
a2enmod ssl
请注意,此处使用的是中文引号和英文引号混合。重定向“/”“https://sub.domain.com/”
<VirtualHost *:80>
#ServerName www.example.com
DocumentRoot /var/www/owncloud
Redirect "/" "https://sub.domain.com/"
好的。
附加说明:如果您不想提取文件,可以阅读手册:
gunzip -cd /usr/share/doc/apache2/README.Debian.gz
zless /usr/share/doc/apache2/README.Debian.gz
(而不是zcat
),这样你可以在终端会话中滚动查看,而无需全部打印出来。 - mwfearnley# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
对我来说这很有效
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
/etc/init.d/httpd reload
或者service httpd reload
进行重新加载。 - Rebecca DessonvilleDocumentRoot /usr/local/apache2/htdocs
这行代码。 - Abel Callejo