重写规则以在URL中强制使用默认语言代码 [英] Rewrite Rule to enforce default lang code in URL

查看:80
本文介绍了重写规则以在URL中强制使用默认语言代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用qtranslate运行WP Blog.它使我可以创建多种语言的帖子.

I am running a WP Blog with qtranslate. It allows me to create posts in multiple languages.

1.不含qtranslate的示例网址:

1 . Example URL without qtranslate:

www.mysite.com/post1

www.mysite.com/post1

2.具有本地化内容的示例URL:

2 . Example URL with localized content:

www.mysite.com/ zh_CN /post1(英语-我的默认设置和后备)

www.mysite.com/en/post1 (english - my default and fallback)

www.mysite.com/ de /post1

www.mysite.com/de/post1

不幸的是,搜索引擎等仍然记得我的旧链接(1.),这些链接仍然可以访问.因此,"www.mysite.com/post1"现在显示了我的英语内容,而没有重定向.但是应该要做的是让301个用户访问"www.mysite.com/en/post1".

Unfortunately search engines etc. still remember my old links (1.) and these are still access-able. So "www.mysite.com/post1" now shows my english content without a redirect. But what it should do is to 301 users to "www.mysite.com/en/post1".

因此,现在我需要一个规则,该规则主要检查URL中是否存在/ en /post1或/ de /post1,否则重定向到后备/ en /post1 URL.有一个例外,因为"/ shop "是一个真实的子目录,并且不需要在语言信息之前加上

So now I would need a rule that basically checks if there is /en/post1 or a /de/post1 in the URL and otherwise redirect to the fallback /en/post1 URL. There is one exception because "/shop" is a real subdirectory and does not need to be preceeded by language information.

-更新-

我做到了!!!这实际上很有趣,但是花了我一段时间才弄清楚.

I did it!!! That was actually fun but took me quite a while to figure out.

RewriteRule ^$ en [R=301,L]
RewriteRule ^([a-z]{2})/{1}$ $1 [R=301,L]
RewriteRule ^([a-zA-Z0-9\-\_]{3,})(/|$)$ en/$1 [R=301,L]

也许有更好的方法可以做到这一点,但确实可以解决问题.谢谢大家的初步帮助!

There are probably better ways to do this but it does the trick. Thanks everyone for the initial help!

推荐答案

您是否尝试过使用通用的RewriteRule,并使用RewriteConds消除了您不希望它重写的情况?

Have you tried using a universal RewriteRule, and using RewriteConds to eliminate the cases that you don't want it to rewrite?

它会像这样:

RewriteCond %{REQUEST_URI} !^/shop(/|$)
RewriteCond %{REQUEST_URI} !^/(en|de)(/|$)
RewriteRule ^(.*)$ /en/$1 [R=301]

但是根据卡米洛的建议,您可以将中间线更改为

But as per Camilo's suggestion, you could change that middle line to

RewriteCond %{REQUEST_URI} !^/[a-z]{2}(/|$)

只要您可以确保帖子的永久链接不少于三个字符(我对WordPress不够熟悉,无法确定是否可行).

as long as you can ensure that your post permalinks won't be less than three characters (I'm not familiar enough with WordPress to be sure if that's possible).

这篇关于重写规则以在URL中强制使用默认语言代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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