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

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

问题描述

我遇到了 .htaccess 的一些问题我的简化文件服务器结构如下:

/index.php/注册.php

我需要使用虚拟网址来处理语言切换:

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

所以我的 .htaccess 应该检查 url 是否包含/en/或/de/并将其翻译成例如./signup.php?language=de

基本上来说,它应该去掉语言标签,但保留文件夹结构的其余部分.

它应该适用于我的本地 xampp 和我的实时服务器.有任何想法吗?谢谢

解决方案

到目前为止您尝试了什么?类似以下的内容应该可以工作:

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

意思很明显:取第二个匹配项($2),即URL中第二个斜杠后面的内容,在它后面推迟一个?language=,然后是第一个匹配项 ($1),即前两个斜杠之间的内容 - 前提是它是 ende 之一.如果您想将任何内容(不仅是 ende)匹配为语言,请将规则更改为:

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

请注意:第一组中的?会使匹配在第一个斜杠处停止,这样你就不会重写eg.

/en/subdir/pippo.php

pippo.php?language=en/subdir

而是要

subdir/pippo.php?language=en

如有疑问,Apache 网站上有优秀文档.><小时>

默认语言

要使所有其他请求(即不以 /en//de/ 开头的 URL)重定向到默认语言(例如 en),首先你要知道你想识别的语言前缀,然后使用以下规则——下面,我假设有三种-3-语言,代码为endefr:

RewriteEngine On重写基数/重写规则 ^(en|de|fr)/(.*)$ $2?language=$1 [L,QSA]重写规则 ^(.*)$ $1?language=en [L,QSA]

如果您没有在第一个 RewriteRule 中定义确切的语言集(例如使用我之前的任何语言"解决方案),嵌套页面上的语言检测可能会失败.规则按此顺序很重要,因为第一个将匹配页面与语言代码然后退出,而第二个将仅在第一个不匹配时应用.

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

/index.php
/signup.php

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

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

so my .htaccess should check if url contains /en/ or /de/ and translate it to eg. /signup.php?language=de

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

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]

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

to

pippo.php?language=en/subdir

but rather to

subdir/pippo.php?language=en

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


Edit: default language

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]

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天全站免登陆