I have a multi-language site with a com TLD with currently the following htaccess rules in place:
RewriteRule ^/?(en|es|de|fr|ja|zh)/$ ?lang=$1&%{QUERY_STRING} [L]
for the home page and
RewriteRule ^/?(en|es|de|fr|ja|zh)/([1-9a-z-]{3,})/$ $2.php?lang=$1&%{QUERY_STRING} [L]
for subpages. This works fine and the user sees something like this:
https://example.com/de/page1/ which is silently translated to
https://example.com/page1.php?lang=de
I now also bought example.de, example.fr, and example.es and they all reside on the same server. Now I would like to change the above structure by showing the visitor
https://example.de/page1/ instead of https://example.com/de/page1/
I tried 2 approaches:
Aproach 1
in the .htaccess file in the example.de public_html folder, I put:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.de$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.de$
RewriteRule ^(.*)$ "https://example.com/de/$1" [P,QSA]
However, this results in a 500 error (mod_proxy is enabled in Apache; as I am on a hosted server, I do not have access to the Apache error logs but only to the error_log in the public_html directory, which does not show anything). If I do the rewrite rule with http instead of https it shows something but all the included javascript and style files (*.js and *.css) produce 404 errors so the page is broken.
Approach 2
Instead of doing the proxy rewrite in the example.de public_html, I configured the domain so that its www directory is the public_html directory in example.com. Without anything else, I am seeing example.de in the browser and it shows the contents of public_html in example.com in the (default) English version. I then tried to add some rewrite rules to the htaccess file in example.com to map example.de to correct language:
RewriteCond %{HTTP_HOST} ^example.de$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.de$
RewriteRule ^(.*)$ /de/$1 [L,QSA]
With this, I again get a 500 error. If I use the full URI https://example.com/de/$1 in the rewrite rule, it works but the rewrite is no longer silent of course.
So my questions:
What am I doing wrong in each approach and which one would be preferable from a security and SEO point of view?
Also, and I have not been able to test this as it is not currently working, how would something like this affect my SSL certificates? I.e., which certificate would be applied? The one for example.de (which is shown to the user address bar) or the one for example.com which is actually served to the user?
Thanks.
2
Answers
The answer is a combination of the comment by M. Eriksson and the answer by SelVazi (thanks to both):
I deleted the new .de,.fr,.es domains as Addon domains to the *.com domain and added them as Aliases for the .com domain instead. As I am using Let's Encrypt on my site, this automatically led to them being included in the SSL certificate for the .com domain.
Without anything else, I then added the following lines to the top of my htaccess file in the .com public_html folder:
This has the advantage that all subsequent rewrites still work as before and none of the internal links which use the syntax
example.com/de/page1/
need to be changed. Excellent solution. Thank you!For completeness, I should add that the reason for the 500 error mentioned in the question was that
SSLProxyEngine
was turned off on the server.You could try something like this :
This will redirect any link of type
https://example.com/de/page1/
tohttps://example.de/page1/
Also
https://example.com/de/
will be redirected tohttps://example.de/