Use .htacess to change site to another language

0

Good afternoon,

I have a site that needs to have the English version, so I thought I had a folder / eng / and just change that url, but the site in Portuguese already uses htacess to have a friendly url that inside the normal root pull the pages inside of a folder called / pages / and then I need to make it when it is in English it will pull inside the folder / eng / pages /.

I'm trying to do but it can not load the page in the right folder.

My .htacess

RewriteEngine On
RewriteRule %{REQUEST_FILENAME} !-f

RewriteRule %{REQUEST_FILENAME} !-d


RewriteRule ^([a-z_-]+)\/?$ index.php?page=$1 [NC,L]
RewriteRule ^eng/([a-z_-]+)\/?$ index.php?page=$1 [NC,L]


<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>

My index

<?php 
    require 'header.php';
    include $url.$page;
    require 'footer.php';
?>

I have an argument that generates the variable $ page

 $page = (isset($_GET['page']) and !empty($_GET['page']))? "pages/{$_GET['page']}.php" : "pages/home.php";  
  if (!file_exists($page)): $page = "pages/404.php"; endif;

If someone can give me a light, I thank them.

    
asked by anonymous 05.11.2018 / 18:13

2 answers

0

You can change the line that redirects to:

RewriteRule ^eng/([a-z_-]+)\/?$ index.php?page=$1&lang=eng [NC,L]

And at the time of searching the file checks the language:

$page = (isset($_GET['lang']) and $_GET['lang'] == "eng")? "eng/" : "";
$page += (isset($_GET['page']) and !empty($_GET['page']))? "pages/{$_GET['page']}.php" : "pages/home.php";
    
05.11.2018 / 18:58
0

At the time I made the suggested change from Gabriel, I changed some things and it worked as I needed however it brought me another problem, I did not want to have to duplicate all the files just change the folder path / page / js or css files between another would like to continue having the base path at the end so I can understand I'm going to put the code more complete, I change the path to my $ url variable.

This is my config

<?php 
  session_start();

  if(isset($_POST['lang'])):
    session_destroy($_SESSION['langAtual']);
    session_destroy($_SESSION['urlAtual']);
    if($_POST['lang']=='eng'):
        $_SESSION['langAtual'] = $_POST['lang']; 
        $_SESSION['urlAtual'] = "http://talktoall.com.br/clientes/factor/eng/";
        echo $_SESSION['urlAtual'];
    elseif($_POST['lang']=='pt'):
        $_SESSION['langAtual'] = $_POST['lang']; 
        $_SESSION['urlAtual'] = "http://talktoall.com.br/clientes/factor/";
        echo $_SESSION['urlAtual'];
    endif;
  endif;

  if(empty($_SESSION['langAtual'])):
    $_SESSION['urlAtual'] = "http://talktoall.com.br/clientes/factor/";
  endif;

  $url = $_SESSION['urlAtual'];


  if(isset($_GET['page']) and !empty($_GET['page'])):    
    if(isset($_SESSION['langAtual']) and $_SESSION['langAtual'] == "eng"):
      $page = "eng/pages/{$_GET['page']}.php";
    else:
      $page = "/pages/{$_GET['page']}.php";
    endif;
  else:
    $page = "pages/home.php";
  endif;

  if ($page == "pages/admin.php"): header("Location: ./index.php"); endif;
  if ($page == "pages/enviar-colaborador.php"): if(empty($_POST['email'])): header('Location: '.$url); exit; endif; endif;
  if (!file_exists($page)): $page = "pages/404.php"; endif;

?>

Here my index

<?php require 'config.php'; ?>
<!doctype html>
<html lang="pt-br">
  <head>
    <meta charset="utf-8">
    <link rel="icon" href="favicon.ico" type="image/x-icon"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=PT+Sans:400,700" rel="stylesheet">  
    <link rel="stylesheet" href="<?=$url?>css/custom.css">   
    <link rel="stylesheet" href="<?=$url?>css/owl.carousel.min.css"> 
    <link rel="stylesheet" href="<?=$url?>css/bootstrap-submenu.min.css"> 
    <meta name="description" content="" />
    <title>Factor</title>
  </head>
  <body>
    <input type="hidden" name="p" id="p" value="<?=$_GET['page']?>">
    <input type="hidden" name="" id="" value="<?=$page?>">
    <input type="hidden" name="" id="" value="<?=$_SESSION['langAtual']?>">
    <input type="hidden" name="url" id="url" value="<?=$url?>">
    <?php 
      require 'header.php';
      include $page;
      require 'footer.php';
    ?>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
    <script src="<?=$url?>js/owl.carousel.min.js"></script>
    <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script><scriptasyncdefersrc="https://maps.googleapis.com/maps/api/js?key=AIzaSyAg8O_C28sofvtTvMldCqUjkU5A5S8l8NI&callback=initMap" type="text/javascript"></script>
    <script src="<?=$url?>js/imageMapResizer.min.js"></script>
    <script src="<?=$url?>js/bootstrap-submenu.min.js"></script>
    <script src="<?=$url?>js/main.js"></script>

  </body>

</html>
    
05.11.2018 / 19:34