Rewriting domain.com to domain.com/en/ – Server Config – SitePoint Forums
I have a bilingual website with English in
domain.com/en/ and Italian a
If anyone visits
domain.com they were initially redirected
domain.com/en/ using a PHP () header function.
Now I wonder if this is the most effective way to achieve this.
I know I can use:
RewriteRule ^$ /en/ [L]
in my .htaccess file to assign the home page to the / en / directory, but if I later try to navigate to (for example) about.php, I end up
domain.com/about.php which gives me a 404 error. I’m shit with RewriteRules and I don’t want to make it / it / inaccessible.
Why do you do it with htaccess?
For example something like this
<?php $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $acceptLang = ['fr', 'it', 'en']; $lang = in_array($lang, $acceptLang) ? $lang : 'en'; header('Location: /'.$lang.'/Index.php'); ?>
1 I like it
I didn’t use this method for the language, but my instinct tells me that if I did, I would type the about.php URL correctly as example.com/it/about.php in my navigation (i.e. checking the current language). while displaying navigation items) instead of trying to fix it when the page appears.
2 I like it
Thanks @Thallius and @tracknut. The reason for thinking on the use of htaccess is that (I think) does not require an additional HTTP request.
However, I have my brain on Sunday right now and will probably have to wait until tomorrow to think about it more deeply.