Wordpress 重写规则以强制执行 URL 中的默认语言代码 [英] Wordpress Rewrite Rule to enforce default lang code in URL

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

问题描述

我正在使用 qtranslate 运行 WP 博客.它允许我创建多种语言的帖子.

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

1.没有 qtranslate 的示例 URL:

1 . Example URL without qtranslate:

2.包含本地化内容的示例 URL:

2 . Example URL with localized content:

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

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 ,否则重定向到后备/zh/post1 网址.有一个例外,因为/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).

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

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