Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 900541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:24:16+00:00 2026-05-15T15:24:16+00:00

Somebody told me that example.com/en is more friendly than example.com/index.php?lang=en . How can I

  • 0

Somebody told me that example.com/en is more friendly than example.com/index.php?lang=en.
How can I change my URL last part from ?lang=en to /en/?

(pd: I don’t want to use gettext or any framework like zend to accomplish this)

This is how I’m internationalizing and localizing my web page:

(live example: alexchen.co.nr/)

lang.php:

<?php
function dlang($Var) {
 if(empty($GLOBALS[$Var])) {
  $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
   $GLOBALS['_SERVER'][$Var]:
   (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
   $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
 }
}

function language() {
 // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
 dlang('HTTP_ACCEPT_LANGUAGE');
 dlang('HTTP_USER_AGENT');

 $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
 $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

 // Try to detect Primary language if several languages are accepted.
 foreach($GLOBALS['_LANG'] as $K) {
  if(strpos($_AL, $K)===0)
   return $K;
 }

 // Try to detect any language if not yet detected.
 foreach($GLOBALS['_LANG'] as $K) {
  if(strpos($_AL, $K)!==false)
   return $K;
 }
 foreach($GLOBALS['_LANG'] as $K) {
  if(preg_match("/[\[\( ]{$K}[;,_\-\)]/",$_UA))
   return $K;
 }

 // Return default language if language is not yet detected.
 return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='zh-tw';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
 'en',
 'es',
 'zh-tw',
 'zh-cn'
);
?>

session.php:

<?php
//proc all page display
include('lang.php'); //language detector
class Session
{
 var $lang;         //Username given on sign-up
 var $url;          //The page url current being viewed
 var $referrer;     //Last recorded site page viewed

 /* Class constructor */
 function Session() {
  $this->time = time();
  $this->startSession();
 }

 function cf($filename) { //function to clean a filename string so it is a valid filename
  $fp = explode('/',$filename);
  $num = count($fp);
  return $fp[$num-1];
 }

 /**
  * startSession - Performs all the actions necessary to
  * initialize this session object. Tries to determine if the
  * the user has logged in already, and sets the variables
  * accordingly. Also takes advantage of this page load to
  * update the active visitors tables.
  */
 function startSession() {
  session_start();   //Tell PHP to start the session

  /* Set referrer page */
  if(isset($_SESSION['url'])) {
   $this->referrer = $search = $this->cf($_SESSION['url']);
  }
  else {
   $this->referrer = "/";
  }

  /* Set current url */
  $this->url = $_SESSION['url'] = $this->cf($_SERVER['PHP_SELF']);

  /* Set user-determined language: */
  //set up languages array:
  //set cookie
  $langs = array('en','es','zh-tw', 'zh-cn');

  if(isset($_GET['lang'])){
   if(in_array($_GET['lang'],$langs)){
    $this->lang =  $_SESSION['lang'] = $_GET['lang'];
    setcookie("lang", $_SESSION['lang'], time() + (3600 * 24 * 30));
   }
  }
  else if(isSet($_COOKIE['lang'])) {
   $_SESSION['lang'] = $_COOKIE['lang'];
  }
  else {
   $_SESSION['lang'] = 'zh-tw';
  }
 }
};
/**
 * Initialize session object - This must be initialized before g
 * the form object because the form uses session variables,
 * which cannot be accessed unless the session has started.
 */
$session = new Session;
?>

localization.php:

    <?php
    include('session.php'); //language detector

    // determine the value of $lang_file according the one in $lang
    $languages = array('en', 'es', 'zh-tw', 'zh-cn');
    if (in_array($_SESSION['lang'], $languages)) {
        $lang_file = 'lang.'.$_SESSION['lang'].'.php';
    } else {
        $lang_file = 'lang.zh-tw.php';
    }

    // translation helper function
    function l($localization) {
     global $lang;
     return $lang[$localization]; }

    // include file for final output
     include_once 'languages/'.$lang_file;
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:24:17+00:00Added an answer on May 15, 2026 at 3:24 pm

    ModRewrite is your friend. Throw this in your .htaccess file. You may want it to go to your session page instead of index, then redirect with PHP. Note, this only works for one page.

    RewriteEngine On
    RewriteBase /
    
    # Redirect languages
    RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.