Apache的mod_rewrite接受语言的子域重定向 [英] Apache mod_rewrite accept language subdomain redirection

查看:127
本文介绍了Apache的mod_rewrite接受语言的子域重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种语言的多语种网站,我使用下列规则将请求重定向到基于浏览器的网站的正确版本接受的语言。

i have a multilingual site with 3 languages and i'm using the following rules to redirect requests to the right version of the website based in browser accept language.

#swedish
RewriteCond %{HTTP:Accept-Language} ^sv.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /sv [L,R=301]

#norwegian bokmal
RewriteCond %{HTTP:Accept-Language} ^nb.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /nb [L,R=301]

#norwegian
RewriteCond %{HTTP:Accept-Language} ^no.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /nb [L,R=301]

#all others go to english
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /en [L,R=301]

我需要升级这个规则做同样的重定向保持子域太

I need to upgrade this rules to do the same redirections keeping subdomains too

Example for a norwegian request:
www.domain.com/subdomain -> www.domain.com/nb/subdomain

我怎样才能做到这一点?

How can i achieve this?

我感谢@faa对我的帮助与现行规则。

I have thank @faa for helping me out with the current rules.

推荐答案

关于页VS子域的问题,您的意见,在这种特殊情况下是无关紧要的。事实是,在你的问题输入的URL为 www.domain.com/subdomain 和,除了名称, /子域显然是一个页面。

UPDATE

Question Description

Regarding the pages vs subdomains issue in your comments, in this specific case is irrelevant. The fact is the incoming URL in your question is: www.domain.com/subdomain and, except for the name, /subdomain is clearly a page.

一个真实的子域的确是一个域名是另一个域名,并且持有该URL格式的一部分,是像 subdomain.domain.com ,所以没有混乱在我的部分。

A real subdomain is indeed a domain that is part of another domain and the URL format that holds it, is something like subdomain.domain.com, so there was no confusion on my part.

这是什么不太清楚,是怎样的重定向将被处理。根据你的问题,但整个路径替换子域,这里有一些例子:

What it is not clear enough, is how the redirection will be handled. According to your question, but replacing "subdomain" with the whole path, here are some examples:


  1. www.domain.com / 应到 www.domain.com/Lang$c$c / ,(previous工作重定向)

  1. www.domain.com/ should go to www.domain.com/LangCode/, (previous working redirection)

www.domain.com/page 应到 www.domain.com/Lang$c$c/page

www.domain.com/page1/page2/page3/etc / 应到 www.domain.com/Langcode /第1页/ 2页/ 3页的/ etc 。这种可能性是没有问题的,但最终能neded

www.domain.com/page1/page2/page3/etc/ should go to www.domain.com/LangCode/page1/page2/page3/etc. This possibility is not in the question, but eventually could be neded.

在这种情况下,在进入和重定向URL的网页应该有相同的名字,但是,虽然在输入的URL不必存在,重定向的URL页面必须存在,因此加载默认的脚本(指数.PHP或index.html,例如)来处理请求。

In those cases, the pages in the incoming and redirected URLs should have the same name, but, although in the incoming URL do not have to exist, in the redirected URL the pages MUST exist and so a loading default script (index.php or index.html, for example) to handle the request.

这意味着,必须有在各页中的脚本受到重定向。我会说,至少有2每种语言。

Which means, there has to be a script in each page subject to redirection. I would say at least 2 for each language.

据一个我的理解,这就是问题的和互补的评论表明,但似乎它不是一个实用的方法。

As far a I understand, that's what the question and complementary comments indicate, but it seems it is not a practical approach.

有一个更好的方法可能是在处理所有的请求的根文件夹一个脚本。这是可与实施例来更好地描述一个想法:

A better approach could be a single script at the root folder that handles all requests. This is an idea that can be better described with examples:


  1. www.domain.com / 在浏览器的地址栏中显示始终在内部却会
    www.domain.com/lang_handler.php?lang=sv

  1. www.domain.com/ always showing in the browser's address bar but going internally to www.domain.com/lang_handler.php?lang=sv or

www.domain.com/page1 / 在浏览器的地址栏中显示始终在内部,但将 www.domain.com/ ?lang_handler.php LANG = SV&安培; target_page1 =第1页

www.domain.com/page1/ always showing in the browser's address bar but going internally to www.domain.com/lang_handler.php?lang=sv&target_page1=page1

这可以在.htaccess mod_rewrite的使用指令来实现。下面是一个例子:

This can be achieved in .htaccess with mod_rewrite directives. Here is an example:

RewriteEngine On
RewriteBase  /

# Set managed languages here, except default (en)
RewriteCond %{HTTP:Accept-Language} ^(sv|ne|no).*$ [NC]

# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI} !lang_handler\.php              [NC]
RewriteRule ^([^/]+)?/?$  lang_handler.php?lang=%1&target_page1=$1  [L,QSA]

# If no match, set English
# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI}  !lang_handler\.php             [NC]
RewriteRule ^([^/]+)?/?$  lang_handler.php?lang=en&target_page1=$1  [L,QSA]

以上规则集映射默默

The above rule set maps silently

http://www.domain.com/ http://www.domain.com/page1

http://www.domain.com/lang_handler.php?lang=Lang$c$c&target_page1=page1

其中,郎code SV NE 没有连接默认

这个例子仅适用于1页或没有任何页面。任意数量的页面虽然可以进行处理,但规则必须作相应的修改。更多的参数和正则表达式组已经被添加到所述的RewriteRules

This example only works for 1 page or no page. Any number of pages can be handled though, but the rules have to be modified accordingly. More parameters and regex groups have to be added to the RewriteRules.

这篇关于Apache的mod_rewrite接受语言的子域重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆