У меня есть VPS в Digital Ocean, на котором размещены два веб-сайта, каждый с несколькими доменами.
Сайт 1: https://jeremiahrose.com/
Альтернативные домены: jeremiahro.se
, jeremiahrose.band
, www.jeremiahrose.com
, www.jeremiahro.se
, www.jeremiahrose.band
Сайт 2: https://thewilliewagtails.band/
Альтернативные домены: thewilliewagtails.com
, thewilliewagtails.com.au
, www.thewilliewagtails.band
, www.thewilliewagtails.com
, www.thewilliewagtails.com.au
,
Я пытаюсь настроить его так, чтобы запрос на любой из альтернативных доменов (http или https) перенаправлялся на безопасный канонический домен для этого веб-сайта. например www.thewilliewagtails.com
перенаправляет на https://thewilliewagtails.band
.
Сейчас работает только наполовину - www.thewilliewagtails.com и thewilliewagtails.com возвращают страницу по умолчанию, и thewilliewagtails.band/ не перенаправляет.
Я хоть убей не могу понять, в чем проблема, я явно не в своей тарелке.
jez@jeremiahrose:/etc/apache2$ ls sites-enabled/
000-default.conf jeremiahrose.com.conf thewilliewagtails.band.conf
<VirtualHost *:80>
ServerAdmin example@gmail.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin example@gmail.com
DocumentRoot /var/www/jeremiahrose.com
<Directory /var/www/jeremiahrose.com/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ServerName jeremiahrose.com
ServerAlias jeremiahro.se
ServerAlias jeremiahrose.band
ServerAlias www.jeremiahrose.com
ServerAlias www.jeremiahro.se
ServerAlias www.jeremiahrose.band
RewriteEngine on
#Added automatically by let's encrypt:
RewriteCond %{SERVER_NAME} =www.jeremiahrose.com [OR]
RewriteCond %{SERVER_NAME} =jeremiahrose.band [OR]
RewriteCond %{SERVER_NAME} =www.jeremiahro.se [OR]
RewriteCond %{SERVER_NAME} =jeremiahro.se [OR]
RewriteCond %{SERVER_NAME} =jeremiahrose.com [OR]
RewriteCond %{SERVER_NAME} =www.jeremiahrose.band
RewriteRule ^ https://jeremiahrose.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin example@gmail.com
DocumentRoot /var/www/jeremiahrose.com
<Directory /var/www/jeremiahrose.com/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ServerName jeremiahrose.com
ServerAlias jeremiahro.se
ServerAlias jeremiahrose.band
ServerAlias www.jeremiahrose.com
ServerAlias www.jeremiahro.se
ServerAlias www.jeremiahrose.band
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.jeremiahrose.com [OR]
RewriteCond %{SERVER_NAME} =jeremiahrose.band [OR]
RewriteCond %{SERVER_NAME} =www.jeremiahro.se [OR]
RewriteCond %{SERVER_NAME} =jeremiahro.se [OR]
RewriteCond %{SERVER_NAME} =www.jeremiahrose.band
RewriteRule ^ https://jeremiahrose.com%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/jeremiahro.se/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/jeremiahro.se/privkey.pem
</VirtualHost>
</IfModule>
<VirtualHost *:80>
ServerAdmin example@gmail.com
DocumentRoot /var/www/thewilliewagtails.band
<Directory /var/www/thewilliewagtails.band/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ServerName thewilliewagtails.band
ServerAlias thewilliewagtails.com.au
ServerAlias thewilliewagtails.com
ServerAlias wwww.thewilliewagtails.com
ServerAlias www.thewilliewagtails.com.au
ServerAlias www.thewilliewagtails.band
RewriteEngine on
#Added automatically by let's encrypt:
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com [OR]
RewriteCond %{SERVER_NAME} =thewilliewagtails.com [OR]
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.band [OR]
RewriteCond %{SERVER_NAME} =thewilliewagtails.com.au [OR]
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com.au
RewriteRule ^ https://thewilliewagtails.band%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin example@gmail.com
DocumentRoot /var/www/thewilliewagtails.band
<Directory /var/www/thewilliewagtails.band/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
ServerName thewilliewagtails.band
ServerAlias thewilliewagtails.com
ServerAlias thewilliewagtails.com.au
ServerAlias www.thewilliewagtails.com
ServerAlias www.thewilliewagtails.com.au
ServerAlias www.thewilliewagtails.band
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com [OR]
RewriteCond %{SERVER_NAME} =thewilliewagtails.com [OR]
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com.au [OR]
RewriteCond %{SERVER_NAME} =thewilliewagtails.com.au [OR]
RewriteCond %{SERVER_NAME} =www.thewilliewagtails.band
RewriteRule ^ https://thewilliewagtails.band%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/jeremiahro.se/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/jeremiahro.se/privkey.pem
</VirtualHost>
</IfModule>
Сейчас работает только наполовину -
www.thewilliewagtails.com
иthewilliewagtails.com
возвращают страницу по умолчанию, иthewilliewagtails.band/
не перенаправляет.
Я просто не могу понять почему thewilliewagtails.com
не будет работать. Однако в вашем <VirtualHost *:80>
контейнер, который соответствует wwww
не www
в ServerAlias
- так что это просто никогда не будет совпадать:
ServerAlias wwww.thewilliewagtails.com
И thewilliewagtails.band
не указывается в вашем HTTP на HTTPS (<VirtualHost *:80>
) перенаправление (отсутствует RewriteCond
директива) - так что да, это не будет перенаправлено в текущем конфиге. Вместо того, чтобы проверять каждое возможное имя хоста, которого не должно быть, вам нужно только проверить, что оно не каноническое имя хоста (см. ниже).
Я полностью согласен с ответом @roaima. Однако вы на полпути со своей текущей конфигурацией, и ваши директивы можно значительно упростить.
Поскольку ваш канонический порт - 443, вам нужен только безусловный mod_alias Redirect
в <VirtualHost *:80>
контейнеры (mod_rewrite не требуется) для эффективного создания перенаправления HTTP на HTTPS. Так, например, следующий блок mod_rewrite:
RewriteEngine on #Added automatically by let's encrypt: RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com [OR] RewriteCond %{SERVER_NAME} =thewilliewagtails.com [OR] RewriteCond %{SERVER_NAME} =www.thewilliewagtails.band [OR] RewriteCond %{SERVER_NAME} =thewilliewagtails.com.au [OR] RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com.au RewriteRule ^ https://thewilliewagtails.band%{REQUEST_URI} [END,NE,R=permanent]
Можно сразу заменить на простой / безусловный mod_alias Redirect
вместо:
Redirect 301 / https://thewilliewagtails.band/
И соответствующий канонический редирект mod_rewrite в <VirtualHost *:443>
можно значительно упростить. Вместо того, чтобы проверять каждое имя хоста, которое может быть, вам нужно только проверить, что это не каноническое имя хоста. Например, следующее:
RewriteEngine on RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com [OR] RewriteCond %{SERVER_NAME} =thewilliewagtails.com [OR] RewriteCond %{SERVER_NAME} =www.thewilliewagtails.com.au [OR] RewriteCond %{SERVER_NAME} =thewilliewagtails.com.au [OR] RewriteCond %{SERVER_NAME} =www.thewilliewagtails.band RewriteRule ^ https://thewilliewagtails.band%{REQUEST_URI} [END,NE,R=permanent]
Сразу можно заменить на более простое:
RewriteCond %{HTTP_HOST} !^thewilliewagtails\.band$
RewriteRule ^ https://thewilliewagtails.band%{REQUEST_URI} [R=permanent,L]
Теперь нужно гораздо меньше помнить / ошибаться.
Тем не менее, я бы, вероятно, все же решил создать отдельный контейнер vHost для неканонических имен хостов порта 443, как предлагает @roaima. Затем вы можете использовать безусловный mod_alias Redirect
и вообще избавьтесь от mod_rewrite.
Более простое решение, не вдаваясь в сложные наборы правил перезаписи.
Для каждого веб-сайта и его псевдонимов:
Redirect
для отправки пользователей на соответствующий канонический vHost.