Answers:
我实际上遵循了这个示例,它对我有用:)
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
/etc/init.d/httpd reload
|| service httpd reload
DocumentRoot /usr/local/apache2/htdocs
不再需要该线路
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
http://www.sslshopper.com/apache-redirect-http-to-https.html
要么
http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html
apache redirect http to https
在此处搜索并降落。这是我在ubuntu上所做的:
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 crt file>
SSLCertificateKeyFile <path to your private key file>
# Rest of your site config
# ...
</VirtualHost>
sudo service apache2 restart
实际上,您的主题属于https://serverfault.com/,但是您仍然可以尝试检查以下.htaccess指令:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
建议不要使用mod_rewrite,而是使用虚拟主机并重定向。
如果您倾向于使用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
参考:Httpd Wiki-RewriteHTTPToHTTPS
如果您要查找301永久重定向,则重定向标志应为
R=301
所以RewriteRule就像
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
服务器版本: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
在文件/etc/apache2/sites-available/000-default.conf中,添加
重定向“ /”“ 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
该代码对我有用。
# ----------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]
我需要这样做的原因很简单,例如将所有http
流量从服务器上默认的apache主页重定向到已服务的页面https
。
由于在配置apache时我还是很环保的,所以我宁愿避免mod_rewrite
直接使用它,而是选择更简单的方法:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
SSLEngine on
...
</VirtualHost>
我之所以喜欢它,是因为它允许我使用apache变量,因此不必指定实际的主机名,因为它只是一个IP地址,没有关联的域名。