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 6827475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:16:12+00:00 2026-05-26T22:16:12+00:00

I have a multi-language page, I want to detect client browser’s language then make

  • 0

I have a multi-languagepage, I want to detect client browser’s language then make a 301 home page or other thing. but I am not sure which way is better for seo. I do not know web spider like which one? Or other way?

<?php 
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE']; 
if (preg_match('/^[zZ][hH]/', $LG)) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/cn/");
exit();} //jump to chinese version
else { 
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/en/");
exit();} //jump to english version
?>

OR

<?php 
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE']; 
if (preg_match('/^[zZ][hH]/', $LG)) {
include ("http://mydomain.com/cn/");
} //include chinese version
else { 
header("HTTP/1.1 301 Moved Permanently");
include ("http://mydomain.com/en/");
} //include english version
?>

OR other way? Thanks.

  • 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-26T22:16:13+00:00Added an answer on May 26, 2026 at 10:16 pm

    As you already assume in your question, you need to parse the Accept-LanguageHTTP/1.1 header, which is available in PHP in $_SERVER['HTTP_ACCEPT_LANGUAGE']. This first needs to be parsed into a structure you can better deal within PHP, like an array:

    /**
     * Convert Accept Language to sorted PHP array
     * 
     * Related HTTP Specs:
     *   <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4>
     *   <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9>
     *
     * @param string $accept header value
     * @return array ([language-range] => qvalue, ...)
     */
    function http_accept_language_array($accept = NULL)
    {
        if (!$accept && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
            $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $accept = (string) $accept;
    
        $pattern = '/([a-z]{1,8}(-[a-z]{1,8})?)(;q=([01](?:\.[0-9]{0,3})?))?(?=$|,[ ]*)/i';
        preg_match_all($pattern, $accept, $matches);
    
        $array = array();
        if (count($matches[1]))
        {
            list(, $ranges,,, $qvals) = $matches;
            # normalize ranges
            foreach ($ranges as &$range)
                $range = strtolower($range);
            unset ($range);
            # set default qvalue 1
            foreach ($qvals as &$qval)
                if ('' === $qval) $qval = '1';
            unset ($qval);        
            $array = array_combine($ranges, $qvals);
            arsort($array, SORT_NUMERIC);
        }
        return $array;
    }
    

    Which for da, en-gb;q=0.8, en;q=0.7 will return:

    array(3) {
      ["da"]    => string(1) "1"
      ["en-gb"] => string(3) "0.8"
      ["en"]    => string(3) "0.7"
    }
    

    You then need to parse this sorted array to find your first match, setting your preference with the en default value:

    $lang = 'en';
    foreach (http_accept_language_array() as $range => $qvalue)
    {
        if (preg_match('/^zh[$-]/', $range))
        {
            $lang = 'cn';
            break;
        }
    }
    

    Finally you can do the redirect based on $lang (or the include or whatever):

    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://mydomain.com/$lang/");
    

    If you’re looking for a ready-made library to deal with this, one existing solution is the Symfony’s HttpFoundation\Request or in PEAR there is HTTP::negotiateLanguage.

    The PHP intl extension has another low-level function that is related, however it’s doesn’t offer an array but a single value: locale_accept_from_http

    Another general resource for more HTTP related information is Advanced handling of HTTP requests in PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-language website. I want a module to add multiple version of
I'm building multi-language support for PHP page. Let's say I have a page url:
I have multi-language website in Drupal, but not all content is translated. I want
I want to have a multi language site. Now, I have 2 domains. The
I want to have my ASP C# application to be multi-language. I was planned
I have added multi-language using the short article below. When you add for example
We're building a multi-language Drupal stack and one of the concerns we have is
I have a problem with Drupal uploads path when in a multi language site.
I have developed about 300 Applications which I would like to provide with multi-language
I have this application that I want to support multi languages. I thought the

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.