Apache - 将 www 和非子域名重定向到同一页面而不会在 TYPO3 中出现 404

Apache - 将 www 和非子域名重定向到同一页面而不会在 TYPO3 中出现 404

所以我有以下问题:当我使用https://www.example.com作为域名,我得到了内容,但是当我使用https://example.com我收到通用的 TYPO3 404 错误页面。

我尝试设置重定向,以 example.com 和 .* 作为源路径,以我的起始页作为目标,还尝试在站点配置中设置 baseVariants。有点奇怪的是,我的正常基础只是 example.com,而不是www.example.comwww.example.com是一个可以正常工作的,而另一个只给出 404。

我需要可靠的https://example.comhttps://example.com配置以获取相同的页面。

我在 Ubuntu 22.04.4 上使用 TYPO v12.4.14,带有 Apache Web 服务器和 PHP 8.1

[这是我尝试过的重定向方法。但这种方法行不通,因为正则表达式没有分隔符。至少这是我最初发表这篇文章以来学到的。] 1

编辑 2:我尝试创建 2 个虚拟主机来将 example.com 重定向到www.example.com。这是我的配置文件:

文件1示例.conf:

<VirtualHost *:80>

    ServerName example.com
    ServerAdmin [email protected]
    Redirect permanent / https://www.example.com

</VirtualHost>

文件 2 example-ssl.conf

<VirtualHost *:443>

    ServerName example.com
    ServerAdmin [email protected]
    Redirect permanent / https://www.example.com

</VirtualHost>

现在它适用于 http:// example. com,但不适用于 https:// example. com

笔记:我之前在 stack overflow 上问过这个问题,并将它复制到这里。

这是我的默认配置 000-default-le-ssl.config

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # 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 example.com

    ServerAdmin [email protected]
    DocumentRoot "/var/www/Example/public/"

    <Directory "/var/www/Example/public">
         Options Indexes FollowSymLinks MultiViews
     AllowOverride All
     Order allow,deny
     allow from all
    </Directory>


    # 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



Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<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
    Redirect permanent / https://www.example.com/

    # 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>

答案1

Redirect permanent / https://www.example.com从文件 2(example-ssl.conf)中删除指令,并添加一条规则以强制在 URI 中使用 www。

您的 example-ssl.conf 配置应如下所示:

<VirtualHost *:443>

    ServerName example.com
    ServerAdmin [email protected]
    
    # Force www in URI 
    <IfModule mod_rewrite.c>
        RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>

</VirtualHost>

RewriteCondition 检查主机是否是你的域名且不带 www。如果是,则重定向到www.example.com。因为这是服务器端,所以您的 404 错误应该会消失。

编辑: 如果 VHost 中的重写不起作用,那么您也可以将其移动到 Docroot 中的 .htaccess 文件中。TYPO3 默认 htaccess 带有重写部分。必须位于第 300 行左右。然后应该看起来像这样:

......

### Begin: Rewriting and Access ###

# You need rewriting, if you use a URL-Rewriting extension (RealURL, CoolUri).

<IfModule mod_rewrite.c>

    # Enable URL rewriting
    RewriteEngine On
    
    # Non www to www
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^(.*) https://www.example.com/$1 [L,R=301]

......

相关内容