我的 000-default.conf 文件(Ubuntu 和 Apache 2.4.34)中有以下内容,但当我访问 www.mydomain.us 或 mydomain.us 时,我没有被重定向到确实可以直接使用的 https 页面https://www.mydomain.us
<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 webmaster@localhost
DocumentRoot /var/www/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
RewriteEngine on
RewriteCond %{SERVER_NAME} =*.mydomain.us
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
错误日志中没有任何内容,访问日志显示,正如所呈现的但并非预期的那样,它正在 /var/www/html 中提供页面。我在 Google 上搜索过,但没有找到与此问题相关的任何有用信息。
提前致谢。
答案1
RewriteCond %{SERVER_NAME} =*.mydomain.us
此条件永远不会匹配,因此重定向永远不会发生=
。条件模式使其成为字典顺序的字符串比较,因此它尝试匹配SERVER_NAME
变量(请求的主机名默认情况下)针对文字字符串*.mydomain.us
。
你可能要用到类似下面的内容:
RewriteEngine on
RewriteCond %{HTTP_HOST} \.example\.com
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [END,NE,R=permanent]
服务器变量始终包含请求标头HTTP_HOST
的值。默认情况下,也包含相同的值,但是,根据指令的值,此变量可以包含指令中使用的值,而不是请求中的值。Host:
SERVER_NAME
UseCanonicalName
ServerName
但是,你并不一定需要 mod_rewrite 来实现这一点,一个简单的 mod_aliasRedirect
可能就足够了:
Redirect 301 / https://www.example.com/
如果您有多个主机,则为每个主机配置一个单独的 vHost。