Search code examples
htmlurlhttp-redirectmultilingual

How to ignore 'en' portion in URL - multi-language website


I am creating a multi-language site which uses a portion of the URL to determine the language, and dynamically fills the page with content using a PHP array. I do this so every language has it's own url:

 https://website.com/en/home.php
 https://website.com/nl/home.php

The issue in this case is that when accessing the english website using https://website.com/en/home.php, the site tries to open the file located at the folder:
(root) / > en > home.php

While the file is actually located at:
(root) / > home.php

Is it possible to "ignore" the language portion in the URL when opening HTML file, but without redirecting so the user still sees it when they navigate to a page.

I tried using the <base> html tag, this did not work.

Any help would be much appreciated!


Solution

  • To create a router you can follow the tutorial here

    You will specifically need to change your .htaccess file to contain

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ index.php [QSA,L]
    

    This will redirect all requests to index.php

    index.php will need to contain:

    <?php
    
    $request = $_SERVER['REDIRECT_URL'];
    
    switch ($request) {
        case '/' :
            require __DIR__ . '/home.php';
            break;
        case '/en/home.php' :
            require __DIR__ . '/home.php';
            break;
        case '' :
            require __DIR__ . '/home.php';
            break;
        default:
            require __DIR__ . '/404.php';
            break;
        }
    

    This way all requests which are redirected to index.php will show a particular page based on the request url according to what you specify in this router.