Apache2 没有按预期进行重写

Apache2 没有按预期进行重写

我在 Digital ocean 上有一个 VPS,托管两个网站,每个网站都有多个域名。

网站 1:https://jeremiahrose.com/

备选 域名 : jeremiahro.se,,,,, jeremiahrose.bandwww.jeremiahrose.comwww.jeremiahro.sewww.jeremiahrose.band

网站2:https://thewilliewagtails.band/

备选 域名 :thewilliewagtails.com,,,,,, thewilliewagtails.com.auwww.thewilliewagtails.bandwww.thewilliewagtails.comwww.thewilliewagtails.com.au

我正在尝试进行设置,以便对任何备用域(http 或 https)的请求都会重定向到该网站的安全规范域。例如www.thewilliewagtails.com重定向到https://thewilliewagtails.band

目前它只发挥了一半的作用——www.thewilliewagtails.comthewilliewagtails.com返回默认页面,并且thewilliewagtails.band/没有重定向。

我无论如何也想不出问题出在哪里,显然我对此束手无策。

jez@jeremiahrose:/etc/apache2$ ls sites-enabled/
000-default.conf  jeremiahrose.com.conf  thewilliewagtails.band.conf

000-默认.conf:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

杰里迈亚玫瑰网

<VirtualHost *:80>
        ServerAdmin [email protected]
        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 [email protected]
        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>

thewilliewagtails.band.conf

<VirtualHost *:80>
        ServerAdmin [email protected]
        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 [email protected]
        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>

答案1

它目前只工作了一半 -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 的回答。但是,您目前的配置已经完成了一部分,您的指令可以大大简化。

Redirect由于您的规范端口是 443,您只需要在容器中使用无条件的 mod_alias <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_aliasRedirect代替:

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]

现在需要记住/出错的事情少了很多。

不过,我可能还是会按照@roaima 的建议为非规范端口 443 主机名创建一个单独的 vHost 容器。然后,您可以使用无条件的 mod_aliasRedirect并完全摆脱 mod_rewrite。

答案2

更简单的解决方案,无需陷入复杂的重写规则集。

对于每个网站及其别名:

  1. 为网站创建规范的 vHost 配置。仅接受单个规范名称。
  2. 创建一个包含所有备用名称的非规范 vHost,并使用简单的Redirect将用户发送到相应的规范 vHost。

相关内容