Rewriting domain.com to domain.com/en/ – Server Config – SitePoint Forums

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 domain.com/it/

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?

I’d rather you write a very small .php or JavaScript file that takes the client’s language and moves to the required language instead of always addressing English.

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.



Source link

Related post

EDUCAUSE 2022: How Data Collection Can Improve Student and Faculty IT Support

EDUCAUSE 2022: How Data Collection Can Improve Student and…

At Indiana University, Gladdin said, to make life easier for students and faculty, they implemented a course template for the Canvas…
UGC, AICTE warn students against online PhD programmes offered by EdTech platforms | Latest News India

UGC, AICTE warn students against online PhD programmes offered…

The University Grants Commission (UGC) and the All India Council for Technical Education (AICTE) on Friday issued a joint advisory against…
UGC, AICTE warn students against online PhD programmes offered by EdTech platforms | Latest News India

UGC, AICTE warn students against online PhD programmes offered…

The University Grants Commission (UGC) and the All India Council for Technical Education (AICTE) on Friday issued a joint advisory against…

Leave a Reply

Your email address will not be published.