1

I'm facing problem with .htaccess and have not idea how to solve it.

Our webpage is exposed on two domains: example.de and example.com.

I would like to redirect user to i18n content when user request root:

  • https://example.com/ URL should be redirected to https://example.com/en
  • https://example.de/ URL should be redirected to https://example.de/de

There should be no redirect when user requests i18n content e.g.:

  • https://example.com/es/*** URL should be redirected to https://example.com/es/***
  • https://example.de/es/*** URL should be redirected to https://example.de/es/***

Can anyone help?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • What about `https://example.com/` (and `example.de/`)? No redirect? "to `https://example.com/en`" - I assume the target URL should have a trailing slash? Is `/en` a physical directory? – MrWhite Feb 03 '21 at 17:45

1 Answers1

0

To redirect only the document root of the respective domains then you could do something like the following using mod_rewrite near the top of your .htaccess file.

RewriteEngine On

# Redirect "example.com/" to "example.com/en"
RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^$ /en [R=302,L]

# Redirect "example.de/" to "example.de/de"
RewriteCond %{HTTP_HOST} =example.de
RewriteRule ^$ /de [R=302,L]

This assumes you have already canonicalised the hostname (ie. no www subdomain).

As per your example, I have omitted the trailing slash on the target URL-path (eg. /en). However, if /en (and /de) are physical subdirectories then you must include the trailing slash, otherwise mod_dir will trigger a secondary 301 (permanent) redirect to append the trailing slash.

The regex ^$ - matches the document root only (an empty URL-path). The CondPattern =example.com is an exact match string comparison (not a regex) that matches against the Host HTTP request header.

Note also that this is a 302 (temporary) redirect. Language redirects generally should be "temporary", allowing the user to change the language. However, if this is intended to be permanent, then change it to a 301, but only once you have confirmed that it's working as intended.

MrWhite
  • 12,647
  • 4
  • 29
  • 41