【问题标题】:Redirect all subdomain URLs to matching main domain URLs - redirects with page paths not working将所有子域 URL 重定向到匹配的主域 URL - 重定向页面路径不起作用
【发布时间】:2021-11-18 17:23:46
【问题描述】:

使用 Ubuntu 16.04 和 Apache 虚拟主机。

我有一个 Wordpress 网站:www.example.com

其他网站通常链接到以下网址:

  • sub1.example.com/page1
  • sub2.example.com/page2

我想将它们重定向到:

  • example.com/page1
  • example.com/page2

我试过了:

RewriteCond %{HTTP_HOST} ^sub1.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^sub1\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

使用这两种配置,我得到以下行为:

  • sub1.example.com 按预期转到 example.com
  • sub1.example.com/page1 转到通用 Apache 404 页面“在此服务器上未找到请求的 URL”。使用 URL sub1.example.com/page1,它不会重定向

我的 DNS 设置涉及指向服务器 IP 的记录,例如 example.com、www、sub1 等。

我的虚拟主机 .conf 文件如下所示:

<VirtualHost *:80>
        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/example.com/public_html
    ServerName example.com
    ServerAlias www.example.com

        <Directory /var/www/example.com/public_html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            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>

</VirtualHost>

我尝试将 sub1.example.com 添加为 ServerAlias 并重新加载 Apache 无济于事。

我该如何解决这个问题?

【问题讨论】:

  • 您实施的规则很好,因此您要么查看缓存结果(您真的确保防止这种情况发生吗?如何?)或者您有其他附加规则在某个地方。重写日志可以让您详细了解重写引擎内部发生的情况。查看文档如何启用它。
  • @arkascha 很确定它没有被缓存,我一直在使用一些不同的重定向分析器工具。应该没有其他规则,我刚刚建立了这个网站。我会研究重写日志记录。
  • 您的子域是否甚至路由到您拥有 .htaccess 文件的目录?在您显示的 VHost 设置中,您只指定了 ServerName example.comServerAlias www.example.com

标签: apache .htaccess redirect virtualhost


【解决方案1】:

我通过为每个子域创建单独的虚拟主机来解决这个问题,每个子域都有自己的文件结构和 .htaccess 文件。

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多