我正在尝试重定向所有对 ardsley73.com 的请求(或者至少www.ardsley73.com) 到www.ardsleyhigh73.com。我尝试在 sites-available/ardsley73.conf 中使用以下重写规则,但启用它并重新启动 apache 后,它不起作用。发生了重定向,但它似乎是我服务器上配置的默认(或第一个)站点。
<VirtualHost *:80>
ServerName ardsley73.com
ServerAlias www.ardsley73.com
RewriteEngine on
RewriteCond "%{HTTP_HOST}" "!^www\.ardsley73\.com" [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "https://www.ardsleyhigh73.com/$1" [L,R,NE]
</VirtualHost>
我做错了什么?除了不太了解 Apache 配置之外…… :)
附加信息
这是 000-default.conf:
<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 [email protected]
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
</VirtualHost>
这是错误显示的站点 286chestnut.conf:
<VirtualHost *:80>
ServerName olbert.com
ServerAlias 286chestnut.olbert.com
Redirect permanent / https://286chestnut.olbert.com/
RewriteEngine on
RewriteCond %{SERVER_NAME} =286chestnut.olbert.com [OR]
RewriteCond %{SERVER_NAME} =olbert.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
这是286chestnut-le-ssl.conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName olbert.com
ServerAlias 286chestnut.olbert.com
DocumentRoot /var/www/olbert.com/286chestnut/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/olbert.com/286chestnut/public_html>
Require all granted
# Allow local .htaccess to override Apache configuration settings
AllowOverride all
</Directory>
# Enable RewriteEngine
RewriteEngine on
RewriteOptions inherit
# Specify which version of PHP to run since we have several
# (added 6/17/2022 per https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-ubuntu-18-04)
<FilesMatch \.php$>
# For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
# Block .svn, .git
RewriteRule \.(svn|git)(/)?$ - [F]
# Recommended: XSS protection
<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/286chestnut.olbert.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/286chestnut.olbert.com/privkey.pem
</VirtualHost>
</IfModule>
我的(有限的)理解是 286chestnut.conf 重定向到 https://,其配置由 286chestnut-le-ssl.conf 控制。
答案1
为了让那些遇到类似问题的人受益......
显然,身份验证(至少在 Edge 和 Chrome 中)是在重定向到其他网站之前进行的。因此,如果“初始”网站没有证书保护,则重定向将在用户同意继续之前发生。
这不是我所期望的。但解决方案相当简单:保护“初始”网站。使用 Let's Encrypt 和 certbot 很容易做到,我就是使用这两个工具。