Use . htacess to change site to another language

Asked

Viewed 125 times

0

Good afternoon,

I have a website that needs to have the English version, so I thought I would have a folder /Eng/ and just change this url but the site in Portuguese I already use htacess to have friendly url that inside the normal root pulls the pages from inside a folder called /pages/ and then I need to make when it is in English it pull inside the folder /Eng/pages/.

I’m trying to do but he can’t 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 anyone can give me a light I’d appreciate it.

  • 1

    It seems that your site is vulnerable to PHP Injection

  • Hello William, thanks for the tip, but taking advantage I always thought I should worry about this type of security when there was some connection with sql, mysql, IE, login or panel this kind of things. I’m thinking wrong?

  • From a look at the link, PHP Injection is different from SQL Injection. With it an attacker could delete all files from your server, even if there is no database connection, it is not very interesting to lose static files

  • Ahhhh show then, I’ll take a look yes, thank you very much.

2 answers

0

You can change the line that redirects to:

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

And when searching for 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";
  • Gabriel worked but I made some changes that caused me another problem. I will answer right below:

0

The moment I made Gabriel’s suggested change, I changed a few things and it worked as needed but it brought me another problem, i didn’t want to have to duplicate all the files just change the path of the folder /page/ files js or css between other would like them to continue having the base path at the end so that I can understand will put the most complete code, I change the path to my variable $url.

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>
    <script src="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>
    <script async defer src="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.