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 badges1 Answer
Reset to default 1In 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.