【问题标题】:I'm trying to redirect /example to example.html in Apache's httpd.conf, but it doesn't work我正在尝试将 /example 重定向到 Apache 的 httpd.conf 中的 example.html,但它不起作用
【发布时间】:2021-09-25 04:42:29
【问题描述】:

这是我尝试在 httpd.conf 中应用的代码:

<VirtualHost *:80>
    ServerName www.test.com
    ServerAlias www.test.com

    DocumentRoot /home/test_com
    ErrorLog /home/logs/apache/www_test/www.test.com-error_log
    CustomLog /home/logs/apache/www_test/www.test.com.-access_log common env=!do_not_log

    # https redirect
    RewriteEngine On

    RewriteCond %{HTTP_HOST} www.test.com
    RewriteCond %{REQUEST_URI} /example
    RewriteRule . https://www.test.com/example.html [R=303,L]

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}

    <Directory "/home/test_com/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

</VirtualHost>

应用代码并重新启动 apache 后,当我转到 /example 时,我得到一个 404 not found 响应。

当我在 (https://htaccess.madewithlove.be) 测试 htaccess 时,输出是我想要的 https://www.test.com/example.html。我想知道问题是什么。

【问题讨论】:

  • 您是通过 HTTP 还是 HTTPS 发出请求?您的所有指令都在 &lt;VirtualHost *:80&gt; 容器中,并且您正在重定向到 HTTPS?

标签: apache .htaccess redirect


【解决方案1】:
RewriteCond %{HTTP_HOST} www.test.com
RewriteCond %{REQUEST_URI} /example
RewriteRule . https://www.test.com/example.html [R=303,L]

当我转到 /example 时,我收到 404 not found 响应。

不确定您的示例中是否丢失了某些内容,但是根据所写的规则,如果您请求/example,它将导致重定向循环,因为它会一次又一次地不断重定向到/example.html。 (除非您实际上是通过 HTTPS 发出请求,在这种情况下指令根本没有做任何事情,您确实会得到 404!)

这是因为第二个条件 %{REQUEST_URI} /example 也匹配目标 URL /example.html。您需要使正则表达式更具体,并且仅匹配 /example,而不是任何仅包含 /example 的 URL。此检查也可以移至RewriteRule 指令(不需要条件)。例如:

RewriteRule ^/example$ https://www.test.com/example.html [R=303,L]

检查HTTP_HOST 的条件似乎没有必要,因为这些指令在www.test.com 的vHost 中,所以它不能是任何其他主机名?

但是,由于此规则位于端口 80 的 vHost 中,因此它仅适用于 HTTP 请求(而非 HTTPS)。如果您实际上是在请求 https://www.test.com/example(即 HTTPS),正如 MWL 工具的测试结果所暗示的那样,那么该指令将永远不会被处理,您确实会得到 404。


RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}

您的 HTTP 到 HTTPS 重定向缺少最后一部分,因此将所有内容重定向到主页。 RewriteRule 指令应为:

RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]

但是,如果您的所有指令都在 vHost:80 容器中,那么重定向到 HTTPS 是没有意义的。

这条规则也比它需要的更复杂。由于您在 vHost:80 容器中,HTTPS 始终是off - 检查是多余的。


ServerName www.test.com
ServerAlias www.test.com

如果您只有一个主机名,那么您不需要两个指令。在这种情况下,只需要ServerName


当我在 (https://htaccess.madewithlove.be) 测试 htaccess 时

您在此处没有.htaccess 文件。您直接在 vHost 容器中使用指令,这是一个不同的 context。指令在此处的行为略有不同。例如,RewriteRule 模式 .(一个点)在此处的结果与在.htaccess 中使用时的结果不同。在 virtualhost 上下文中,每个请求都成功,而在.htaccess 中,除了主页(可能是意图在这里)。

但是,MWL .htaccess 测试器仅发出“单次”请求(或单次通过文件)。它不会像在真实服务器环境中那样“遵循”重定向或多次通过文件。所以它不能捕获重定向/重写循环——这是一个常见的错误原因。

【讨论】:

    【解决方案2】:

    RewriteRule 右边的部分是目标文件,尝试改变:

    RewriteCond %{REQUEST_URI} /example
    

    收件人:

     RewriteCond %{REQUEST_URI} /example.html
    

    【讨论】:

    • 虽然这会让它更多错! OP 想要重定向到/example.html。您提出更改的 条件 将在重定向到 /example.html 之前检查请求是否已经是 /example.html(除了这是一个正则表达式)。使用 /example 是正确的选择 - 除非,如前所述,这是一个正则表达式,因此匹配太多(它需要更具限制性,以便它匹配 /example)。不幸的是,OPs 代码还有其他几个问题也会阻止它按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2016-11-01
    • 2011-03-23
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2020-11-17
    相关资源
    最近更新 更多