简体   繁体   中英

.htaccess Handle uri with php

I have a codebase that uses the logic,

  if($_SERVER['HTTP_HOST'] == 'fr.example.com')
    {
        $_SESSION["lang"] = "fr";
    }
    elseif($_SERVER['HTTP_HOST'] == 'en.example.com')
    {
        $_SESSION["lang"] = "en";
    }

I want:

www.example.com should serve english

www.example.com/fr should serve french.

All the text is served from the database.

How can I use same index.php to serve english and french content based on what language is there in the URI?

Since you want to handle this in PHP anyway, there is no need for an .htaccess file. Just use $_SERVER['REQUEST_URI'] , eg

$uri = $_SERVER['REQUEST_URI'];
if (stripos($uri, "/fr/") === 0) {
    // handle french
} elseif (stripos($uri, "/de/") === 0) {
    // handle german
} else {
    // handle english
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM