对于语言检测的.htaccess规则 [英] .htaccess rule for language detection

查看:226
本文介绍了对于语言检测的.htaccess规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能和一些问题的.htaccess 我简单的文件服务器结构如下:

i'm stuck with some problem with .htaccess my simplified file server structure is the following:

/index.php
/signup.php

我需要使用虚拟URL来工作的处理语言切换:

i need to work with virtual urls for handling the language switch:

www.mydomain.com/en/signup.php   
www.mydomain.com/de/signup.php

所以我的.htaccess应该检查是否URL包含/ EN /或/德/并转换成如。 /signup.php?language=de

基本上讲,它应该剥离语言的标签,但保留的文件夹结构的其余部分。

basically spoken, it should strip the language tag but keep the rest of the folder structure.

它应该在我的两个地方XAMPP和我活的服务器上。 有任何想法吗?谢谢

it should work on both my local xampp and on my live server. any ideas? thanks

推荐答案

你尝试过什么这么远吗?类似下面应该工作:

What have you tried so far? Something like the following should work:

RewriteRule ^/(en|de)/(.*)$  $2?language=$1 [L]

的含义是相当明显的:取第二场比赛( $ 2 ),这是什么来的网址第二斜线后,推迟 ?语言= 之后,然后在第一场比赛( $ 1 ),也就是说,什么是前两个斜线之间 - 只要它是一个连接。 如果你想匹配任何东西(不仅仅是连接)的语言,然后更改规则:

The meaning is rather obvious: take the second match ($2), that is what comes after the second slash in the URL, postpone a ?language= after it, and then the first match ($1), that is, what's between the first two slashes - provided it's one of en or de. If you wanna match anything (not only en or de) as language, then change the rule to:

RewriteRule ^/(.+?)/(.*)$  $2?language=$1 [L]

请注意:在 在第一组将进行匹配停在第一个斜线,这样你就不会改写如。

Please note: the ? in the first group will make the match stop at first slash, so that you won't rewrite eg.

/en/subdir/pippo.php

pippo.php?language=en/subdir

,而是

subdir/pippo.php?language=en

如有疑问,有href="http://httpd.apache.org/docs/2.2/rewrite/remapping.html" rel="nofollow">优秀的文​​档在一个

In case of doubts, there is an excellent documentation in the Apache website.

编辑:默认语言

为使所有其他请求(即网址不是以 / EN / /德/ )重定向到一个默认的语言(比如连接),首先你要知道你要认识到语言prefixes,然后用下面的规则 - 下面,我米假设有三个-3-语言,用codeS 连接 FR

To make all other requests (that is, URLs not starting with /en/ or /de/) redirect to a default language (say en), first you have to know the language prefixes you want to recognize, and then use the following rules - below, I'm assuming there are three -3- languages, with codes en,de and fr:

RewriteEngine On
RewriteBase /

RewriteRule ^(en|de|fr)/(.*)$  $2?language=$1 [L,QSA]
RewriteRule ^(.*)$  $1?language=en [L,QSA]

如果您不定义在第一重写规则(例如,使用我的previous任何语言的解决方案),语言检测可能会失败上设置的确切的语言嵌套页面。这是非常重要的规则是这个顺序,因为第一个将匹配的语言code,然后退出页面,而第二个将仅应用于如果第一个不匹配。

If you don't define the exact language set in the first RewriteRule (eg using my previous "any language" solution), language detection could fail on nested pages. It's important that the rules are in this order because the first one will match pages with the language code and then exit, whilst the second one will be applied only if the first does not match.

这篇关于对于语言检测的.htaccess规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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