首页 > 解决方案 > 有条件的接受语言重定向指令可能吗?

问题描述

我想达到什么目的?

/de/如果调用主页 ( /) 并且 HTTP 标头Accept-Languagede访问者浏览器的开头但仅针对特定域,我想重定向到。

这是我的出发点:

由于我有许多特定于域的重定向规则,因此我有多个<If>指令。下面的代码片段被减少到最低限度。

<IfModule mod_rewrite.c>
    RewriteEngine on

    # Redirect serveral domain aliases
    RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^(www\.)?my\-domain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain\.com$
    RewriteRule .* https://www.mywebsite.com/en/products/overview/ [R=301,L]

    RewriteCond %{HTTP_HOST} ^(karriere|jobs)\.areal\-domain.at$
    RewriteRule .* https://www.mywebsite.com/de/ueber-uns/karriere/ [R=301,L]

    # Redirect language DE for homepage only of www.mywebsite.com
    # (the following rule is  outside of the following otherwise it will not work actually
    RewriteCond %{REQUEST_URI} ^/?$
    RewriteCond %{HTTP:Accept-Language} ^de [NC]
    RewriteRule ^$ /de/ [R=301,QSA,L]

    <If "req('Host') =~ /(live.mywebsite.com|www.mywebsite.com|mywebsite.com|mywebsite.docker)/">
        RedirectMatch 301 ^/$ /en/
        RedirectMatch 301 ^/contact(/)?$ /en/about-us/contact/
        RedirectMatch 301 ^/customer-support(/)?$ /en/service-and-support/
    </If>

</IfModule>

这是我的问题:

如果我将语言重定向的重写放入<If>指令中,apache v2.4.38 服务器确实总是发送 HTTP 状态代码 500,如下一个片段所示:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # ...

    <If "req('Host') =~ /(live.mywebsite.com|www.mywebsite.com|mywebsite.com|mywebsite.docker)/">
        # Redirect language DE for homepage only of www.mywebsite.com
        RewriteCond %{REQUEST_URI} ^/?$
        RewriteCond %{HTTP:Accept-Language} ^de [NC]
        RewriteRule ^$ /de/ [R=301,QSA,L]

        RedirectMatch 301 ^/$ /en/
        RedirectMatch 301 ^/contact(/)?$ /en/about-us/contact/
        RedirectMatch 301 ^/customer-support(/)?$ /en/service-and-support/
    </If>

</IfModule>

这种行为的原因是什么?

我阅读了文档,但找不到有关我的方案的任何信息。

标签: apache.htaccessurl-redirectionapache2.4

解决方案


推荐阅读