1

I'm trying to redirect language URLs that end with /?lang=da, /?lang=de and /?lang=nl to the same URL but ending in /?lang=en.

So

www.example.com/accommodation/hotel-room-1/?lang=da

should result in

www.example.com/accommodation/hotel-room-1/?lang=en

etc.

Is there a way to use wildcards for this?

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Kneops
  • 13
  • 3
  • Rather than redirecting in your web server consider setting a default language in the code that selects which language to display when a unsupported `lang=locale` is used. Then adding support for additional languages in the future won’t need any server configuration changes – Bob Jun 12 '20 at 08:58
  • But would this not be good for Google and SEO? The pages of these languages are +15 years old and lots of links in the web that link specifically to these pages. That's why I was looking for a 301 redirect. – Kneops Jun 12 '20 at 09:07
  • Is the URL-path fixed (ie. always `/accommodation/hotel-room-1/`) or do you need a "wildcard" for that as well? – MrWhite Jun 12 '20 at 11:04
  • 1
    Yes for that as well. I don't know if that's even possible. You can take a look at the site: www.ullstorp.se. So only Swedisch and Eglish should remain. – Kneops Jun 12 '20 at 11:14

1 Answers1

0

If these language versions don't exist anymore then you can implement a redirect using mod_rewrite in your root .htaccess file.

For example, near the top of your .htaccess file try the following:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule (.*) /$1?lang=en [R=301,L]

This 301 redirects /<anything>?lang=da (or de or nl) to the same URL-path but with lang=en. As in your example, /accommodation/hotel-room-1/?lang=da redirects to /accommodation/hotel-room-1/?lang=en.

The $1 backreference captures the URL-path from the requested URL.

Note that it's preferable to first test with 302 (temporary) redirects to avoid caching any erroneous redirects and only change to 301 when you are sure it's working as intended.

MrWhite
  • 11,643
  • 4
  • 25
  • 40