Rewrite URL with .htacces

0

I'm with a project using the model-view-controller and I want to make the url "friendly". For this I want to change part of the url to rewrite the part of the url that puts /? Zone = menu-javascript and leave me alone / menu-javascript.

I understand that I have to do this through a .htacces file. This file I have and has this code:

#Activar RewriteEngine
 RewriteEngine on

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 #Reescribir la URL solicitada por el usuario
 #Entrada: seccion partiendo desde raiz
 #Salida: ?zona=seccion

 RewriteRule ^([a-zAZ0-9]+)$ ?zona=$1 

 ErrorDocument 404 /

I make the request to each zone in the following way:

<?php
 session_start();

 $componente = (isset($_GET['zona'])) ? $_GET['zona'] : 'home';

 function loader($componente) {

  ob_start();
  include 'componentes/' . $componente . '/controller.php';
  $buffer = ob_get_clean();
  return $buffer;
  }

I think the pattern is correct but it does not work for me. Any suggestions? Thanks.

    
asked by 22.05.2018 в 16:24
source

1 answer

0

By not including the script in the pattern it will not take you "menu-javascript", you can include everything and then filter in $_GET

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ ?zona=$1 [L]
ErrorDocument 404 /
<?php
 session_start();

 $componentesValidos = array('menu-javascript', 'menu-otro', 'home');

 $componente = 
    ( isset($_GET['zona']) && 
      in_array($_GET['ZONA'], $componentesValidos) ) ? 
      $_GET['zona'] : 'home';

 function loader($componente) {
   $buffer = '';
   $componenteFile = 'componentes/' . $componente . '/controller.php';
   if(file_exists($componenteFile)) :
     ob_start();
     include ($componenteFile);
     $buffer = ob_get_clean();
   endif;
   return $buffer;
 }

edited added RewriteBase to bypass certain situations where the " basedir " of RewriteRule is lost. in this case it is / if you move the project within a subfolder will be the path relative to the domain.

    
answered by 22.05.2018 / 21:20
source