我有一个安装了 Ubuntu 和 Apache 的 AWS EC2 实例。
此实例的流量始终通过端口 80,因为它前面有一个处理 https 请求的负载均衡器。
我想确保即使有人尝试通过常规 http 浏览它,它也会自动重定向到与 https 相同的 url。
这是我的.htaccess:
Options -Indexes
#Set the response headers
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "*"
</IfModule>
<IfModule mod_rewrite.c>
#Enable the Rewrite Engine
RewriteEngine On
#Redirect to the Https site always.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
这是虚拟主机配置文件:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin admin@my_domain.com
ServerName dev_brands.my_domain.com
ServerAlias new_dev_brands.my_domain.com
DocumentRoot /var/www/dev_brands.my_domain.com/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<Directory /var/www/dev_brands.my_domain.com>
AllowOverride All
</Directory>
我知道 ..htaccess 文件正在被处理,因为如果我只是输入随机文本 - 它会导致 500 错误。
甚至当我尝试将流量重定向至 Google 时也不起作用。