Htaccess redirect after changing Language URL format

After have changed the URL language format from?lang=en?lang=rutoI'd like to redirect the former URL versions

After have changed the URL language format from

/?lang=en
/?lang=ru

to

/
/

I'd like to redirect the former URL versions to the actual ones.

I found this code snippet online and apparently it partially solves my problem.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} lang=en
# exclude all requests starting with /wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-admin/.*$
RewriteRule ^(.*) /en/$1? [L,R=301]
</IfModule>

However, it is designed for lang=en only!

How could I also include lang=ru in it so that it will redirect both lang=en and lang=ru?

Thank you in advance for any valuable hint!

After have changed the URL language format from

http://my-site/name-of-page/?lang=en
http://my-site/name-of-page/?lang=ru

to

http://my-site/en/name-of-page/
http://my-site/ru/name-of-page/

I'd like to redirect the former URL versions to the actual ones.

I found this code snippet online and apparently it partially solves my problem.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} lang=en
# exclude all requests starting with /wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-admin/.*$
RewriteRule ^(.*) /en/$1? [L,R=301]
</IfModule>

However, it is designed for lang=en only!

How could I also include lang=ru in it so that it will redirect both lang=en and lang=ru?

Thank you in advance for any valuable hint!

Share Improve this question asked Sep 20, 2019 at 10:32 RomanRoman 231 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

In order to catch either lang=en or lang=ru you can change those directives like this:

RewriteCond %{QUERY_STRING} lang=(en|ru)
# exclude all requests starting with /wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteRule (.*) /%1/$1? [L,R=302]

The (en|ru) part matches either en or ru and the surrounding parentheses make this a capturing group that can be referenced later.

The %1 (note the %, not $) in the RewriteRule substitution is a backreference to the captured group mentioned above. So, %1 holds either en or ru.

The trailing .*$ on the end of !^/wp-admin/.*$ is superfluous. As is the ^ prefix on the RewriteRule pattern (.*) - since regex is greedy by default.

Test first with a 302 (temporary) redirect and only change to 301 (permanent) when you are sure this is working OK - to avoid caching issues.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745142202a4613480.html

相关推荐

  • Htaccess redirect after changing Language URL format

    After have changed the URL language format from?lang=en?lang=rutoI'd like to redirect the former URL versions

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信