skip to Main Content

I’m still struggling to figure out how exactly to redirect the user if somebody is calling my domain en.sparkm4n.de but is supposed to get www.sparkm4n.de/index.php?lang=en I want to keep the subdomain but I want that the script of .htaccess is exactly calling the php script which translates all the content inside of the subdomain. I have so far 3 subdomain like en.sparkm4n.de for english, fr.sparkm4n.de for french, es.sparkm4n.de for spanish and the top level domain which is www is german basically. I tried so far this:

RewriteEngine on
RewriteCond %{QUERY_STRING} !^en=
RewriteCond %{HTTP_HOST} ^(?!www.)(.+).sparkm4n.de$
RewriteRule ^(/?)$ $1?lang=%1en

I’m not quite certain if it does the right thing as I’m totally new to .htaccess regex.
What will be better solution to have 3 subdomains included in one .htaccess doing exactly fetching the content from toplevel get query into subdomain? There is no content inside of subfolder but it supposed to redirect to toplevel domain with all the content and scripts

2

Answers


  1. Chosen as BEST ANSWER

    I solved the question by using proposed $_SERVER['HTTP_HOST']; Directly in the language script file I implemented this:

    <?php
      session_start();
      header('Cache-control: private');
      list($subdomain,$host) = explode('.', $_SERVER["HTTP_HOST"]);
      if($subdomain == 'en'){
        $lang = 'en';
      } else if($subdomain == 'es'){
        $lang = 'es';
      } elseif($subdomain == 'fr'){
        $lang = 'fr';
      } else{
        $lang = 'de';
      }
    
      if(isSet($lang)){
      $_SESSION['lang'] = $lang;
        setcookie('lang', $lang, time() + (3600 * 24 * 30));
      }
      else if(isSet($_SESSION['lang'])){
        $lang = $_SESSION['lang'];
      }
      else if(isSet($_COOKIE['lang'])){
        $lang = $_COOKIE['lang'];
      }
      else{
        $lang = 'de';
      }
    
      switch ($lang) {
        case 'en':
          $lang_file = 'en.php';
        break; 
        case 'fr':
          $lang_file = 'fr.php';
        break; 
        case 'es':
          $lang_file = 'es.php';
        break; 
        default:
        $lang_file = 'de.php';
      }
    

  2. Have it like this:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} !(?:^|&)lang= [NC]
    RewriteCond %{HTTP_HOST} ^(?!www.)(.+).sparkm4n.de$ [NC]
    RewriteRule ^ %{REQUEST_URI}?lang=%1 [L,QSA]
    

    Note that this will rewrite every page with an additional lang parameter if it is missing. This will not show lang parameter in browser URL since we are not doing a full (external) redirect using R flag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search