drupal multisite htaccess:在一个子网站上将所有语言重写为默认语言

时间:2021-10-18 14:58:30
  • I have a Drupal 6 multisite, with 2 domains (www.example.com and www.domain.com), sharing some common content.

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

  • 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).

    域example.com有三种语言(EN,FR,NL)。语言由路径前缀(/ en,/ fr,/ nl)设置。另一个域domain.com只使用一种语言(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.

    问题:在很多情况下,domain.com以错误的语言显示,即使没有填写路径前缀。不知何故,它似​​乎默认为EN,虽然它并不总是这样做 - 行为似乎不是很是一致的。

  • 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.

    解决方案(至少我希望):因为我不是Drupal开发人员(我从前同事那里得到了网站)我不知道如何在Drupal中解决这个问题,所以我认为解决这个问题的最佳方法是为.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.

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

  • Some examples:

    一些例子:

    • www.domain.com/fr/some-title needs to be rewritten to www.domain.com/nl/some-title
    • www.domain.com/fr/some-title需要重写为www.domain.com/nl/some-title
    • www.domain.com/node/1975 needs to be rewritten to www.domain.com/nl/node/1975
    • www.domain.com/node/1975需要重写为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:

并且期望这只是为所有路径添加/ nl /(因此不是完整的解决方案,因为/ fr / some-title将变为/ nl / fr / some-title) - 然而,快速测试向我显示:

  • /fr/some-title is rewritten to /nl/some-title (which is what I need, but not what I expected)
  • / fr / some-title被重写为/ nl / some-title(这是我需要的,但不是我的预期)
  • /some-title is not rewritten

    / some-title不会被重写

  • 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?

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

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

    仅仅为了完整起见:现场网站是www.cinemazuid.be

2 个解决方案

#1


3  

If this rule

如果这个规则

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

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.

在你的.htaccess文件中,我很惊讶它作为领先/总是被剥离,所以理论上它应该永远不会匹配任何请求。

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

如果您希望为那些未指定语言的请求强制使用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]

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

如果您不想重定向,只需删除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.

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

#2


0  

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

#1


3  

If this rule

如果这个规则

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

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.

在你的.htaccess文件中,我很惊讶它作为领先/总是被剥离,所以理论上它应该永远不会匹配任何请求。

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

如果您希望为那些未指定语言的请求强制使用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]

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

如果您不想重定向,只需删除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.

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

#2


0  

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