我正在寻找正确的方法来处理redirect不安全(通过端口80)的请求,以确保(端口443)与Apache服务器configuration。 我不想把这个在我的.htaccess文件中。
目前我在我的httpd.conf文件中有这个:
ServerName example.com ServerAlias *.example.com RewriteEngine On RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ RewriteRule ^(.*)$ https://%1.example.com/$1 [R=302,L]
目标是做一个通配符(在子域和子文件夹)redirect。 具体来说,这里有一些主要的用例:
- http://subdomain.example.com到https://subdomain.example.com - http://example.com到https://www.example.com - http://www.example.com/contact/到https://www.example.com/contact - http://subdomain.example.com/contact/到https://subdomain.example.com/contact
简而言之,只要用https来replacehttp ,只要满足两个条件:
我已经尝试了许多不同的方法,但似乎没有捕获子域和子文件夹的所有变化,让我吃惊的是。
有任何想法吗? 谢谢!
一个简单的虚拟主机是捕获所有HTTP请求的最好方法。 那么你不能不小心redirectHTTPS请求。 就像是。
<VirtualHost *:80> ServerName example.com ServerAlias *.example.com # Redirect subdomains. RewriteEngine on RewriteCond %{HTTP_HOST} (\w+.example.com) RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301] # Everything else to www.example.com preserving URI path Redirect 301 / https://www.example.com/ </VirtualHost
redirect到HTTPS:
RewriteEngine On
RewriteCond%{HTTPS}closures
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
但你需要有一个虚拟主机的SSL:
NameVirtualHost *:443
<VirtualHost *:443>
ServerName example.com ServerAlias www.example.com SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</ VirtualHost>
使用mod重写指令的解释
RewriteEngine On —它会打开Apache服务器上的mod_rewrite引擎
RewriteCond%{HTTPS}closures——
如果连接使用SSL / TLS,将包含文本“on”,否则包含“off”文本。 (无论是否加载mod_ssl,都可以安全地使用此variables)。 如果连接不是ssl / tls,将会评估为真实的条件
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
这是为了形成最初由客户端请求的url,但在https中