drupal 多站点 htaccess:将一个子站点上的所有语言重写为默认语言

发布于 2024-12-23 10:23:38 字数 1608 浏览 1 评论 0原文

  • 我有一个 Drupal 6 多站点,有 2 个域(www.example.com 和 www.domain.com),共享一些常见内容。

  • 域名 example.com 使用三种语言(EN、FR、NL)。语言由路径前缀(/en、/fr、/nl)设置。另一个域domain.com 仅使用一种语言(NL)。

  • 问题:在很多情况下,即使没有填写路径前缀,domain.com 也会以错误的语言显示。不知何故,它似​​乎默认为 EN,尽管并非总是如此那 - 行为似乎不太一致。

  • 解决方案(至少我希望):因为我不是 Drupal 开发人员(我从前同事那里继承了该网站),所以我不知道如何在 Drupal 中解决这个问题,所以我我认为解决这个问题的最佳方法是向 .htaccess 添加一些重写规则。

  • 我也不是 htaccess/regex 专家,并且无法让它工作。您可以在下面找到我当前的重写规则,非常欢迎任何帮助或建议。

  • 一些例子:

    • www.domain.com/fr/some-title 需要重写为 www.domain.com/nl/some-title
    • www.domain.com/node/1975 需要重写为 www.domain.com/nl/node/1975

这些是已经存在的重写规则:

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

我尝试添加这:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^/(.*)$ /nl/$1

并希望这只是为了将 /nl/ 添加到所有路径(因此不是完整的解决方案,因为 /fr/some-title 将变为 /nl/fr/some-title) - 但是,快速测试表明:

  • /fr/some-title 被重写为 /nl/some-title (这是我需要的,但不是我期望的)
  • /some-title 不是重写

  • 问题:有什么想法可能是错误的吗?或者这可能是由其他(Drupal)设置引起的?或者有更好的方法来解决我的问题吗?

  • 为了完整起见:实时网站是 www.cinemazuid.be

  • I have a Drupal 6 multisite, with 2 domains (www.example.com and www.domain.com), sharing some common content.

  • The domain example.com is in three languages (EN, FR, NL). Languages are set by path prefix (/en, /fr, /nl). The other domain domain.com is just in one language (NL).

  • The problem: on many occasions domain.com is shown in the wrong language, even if no path prefix is filled in. Somehow it seems to default to EN, though it doesn't always do that - behaviour doesn't seem to be very consistent.

  • The solution (at least I hope): since I'm not a Drupal developer (I inhereted the site from a former colleague) I have no idea how to fix this in Drupal, so I thought the best way to fix it would be to add some rewrite rules to .htaccess.

  • I'm no htaccess/regex expert either, and can't get it working. You can find my current rewrite rules below, any help or suggestions are most welcome.

  • Some examples:

    • www.domain.com/fr/some-title needs to be rewritten to www.domain.com/nl/some-title
    • www.domain.com/node/1975 needs to be rewritten to www.domain.com/nl/node/1975

These are the rewrite rules that were already there:

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I tried adding this:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^/(.*)$ /nl/$1

and would expect this just to prepend /nl/ to all paths (thus not being a full solution since /fr/some-title would become /nl/fr/some-title) - however, a quick test shows me that:

  • /fr/some-title is rewritten to /nl/some-title (which is what I need, but not what I expected)
  • /some-title is not rewritten

  • The question: any ideas what might be wrong? Or could this be caused by other (Drupal) settings? Or is there a better way to solve my problem?

  • Just for the sake of completeness: the live website is www.cinemazuid.be

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-12-30 10:23:38

如果这个规则

重写规则^/(.*)$ /nl/$1

在您的 .htaccess 文件中,我很惊讶它的工作原理,因为前导 / 总是被删除,所以理论上它应该永远不会匹配任何请求。

如果您希望对未指定语言的请求强制使用默认语言 NL,请将以下规则添加到 .htaccess 文件的顶部,位于任何现有规则之前

#if request is for existing file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
#then stop processing
RewriteRule .* - [L]

#replace fr with nl. This rule
RewriteRule ^fr/(.*)$ /nl/$1 [L,R=301]

#if the request does not have a language of en or nl
RewriteCond %{REQUEST_URI} !^/(en|nl)/ [NC]
#redirect with nl as default language
RewriteRule .+ /nl%{REQUEST_URI} [L,R=301]

。想要重定向,只需删除

我在上面编辑的代码,将 /fr/some-title 替换为 /nl/some-title/ >。
L 标志告诉 mod_rewrite 停止处理进一步的规则,这通常是您想要的,除非您有另一个规则需要进一步处理当前请求。

If this rule

RewriteRule ^/(.*)$ /nl/$1

is in your .htaccess file, I am surprised that it works as the leading / is always stripped out, so it should theoretically never match any request.

If your desire is to force a default language of NL for those requests that do not specify a language, then add the following rules to the top of your .htaccess file, before any existing rules

#if request is for existing file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
#then stop processing
RewriteRule .* - [L]

#replace fr with nl. This rule
RewriteRule ^fr/(.*)$ /nl/$1 [L,R=301]

#if the request does not have a language of en or nl
RewriteCond %{REQUEST_URI} !^/(en|nl)/ [NC]
#redirect with nl as default language
RewriteRule .+ /nl%{REQUEST_URI} [L,R=301]

If you do not want to redirect, just drop the R=301

I edited code above to replace /fr/some-title with /nl/some-title/.
The L flag tells mod_rewrite to stop processing further rules, which is usually what you want, unless you have another rule that needs to further process the current request.

溺孤伤于心 2024-12-30 10:23:38
#redirect /fr/* and /en/* to /*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(en|fr)/(.*)$ /$2 [R,L]

#internally rewrite /* to /nl/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond $1 !^nl/$ [NC]
RewriteRule ^(.*)$ /nl/$1

#drupal code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
#redirect /fr/* and /en/* to /*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(en|fr)/(.*)$ /$2 [R,L]

#internally rewrite /* to /nl/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond $1 !^nl/$ [NC]
RewriteRule ^(.*)$ /nl/$1

#drupal code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文