Help with .htaccess - Url Friendly

6

I have a website developed in and now I want to use URL friendly to my links.

I have for example the traditional link:

midominio.com/portal/public/oficinas.php?seccion=24

24 is to the section: office

On this page it shows me the list of offices:

  • Logistics
  • Customer service

I try to show myself in friendly URL:

midominio.com/portal/public/oficinas

Now I also have this URL:

midominio.com/portal/public/oficinasDetalle.php?seccion=24&categoria=56

Where my section is: offices and category is

I try to make it my URL:

midominio.com/portal/public/oficinas/logistica

There are 2 links one that shows me the list of offices and the other one that shows me the detail of the office, as it would be in .htaccess to make friendly links as well as I explain lines up.

For example, when the user types a link that does not exist, I get a 403 error.

Actually I already did this:

Option +FollowSymLinks
RewriteEngine on
RewriteRule ^.*$ ./index.php

Now in my index.php found in the portal root:

if ($parte_url=="oficinas") {
  include_once(public/oficinas) 
}else {
  echo "error 404";
}

My link looks like this:

misitio.com/portal/oficinas

Even there everything perfect shows me the list of offices, as well as I wish, now I have this link where it shows me the detail of an office

midominio.com/portal/public/oficinasDetalle.php?seccion=24&categoria=56

And I would like it to be like this:

misitio.com/portal/oficinas/logistica

Please help .. I just need that to publish it on the web.

or if you have another idea of how to do it, if you can help me implement it

    
asked by MasSoft 27.04.2016 в 00:35
source

2 answers

2

Add this to your .htaccess

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=([^&\ ]+)
RewriteRule ^/?$ /%1? [L,R=301]

Here you are indicating that you will show the url only with the content of the regular expression

[^&\ ]+
    
answered by 28.07.2016 в 23:13
-1

A generic example to simplify the htaccess rules and give work to the index.php

first we capture everything that comes in the url after the server and we pass it to index.php (the first two conditions is that it does not do this if there already exists a folder or file with that name)

.htaccess

RewriteEngine On
Rewritecond %{REQUEST_FILENAME} !-f
Rewritecond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]

Then in the index.php we transform that route that comes in the form of a string to an array and "subtract" the folders where the same php is hosted (from the things that come from the apache)

<?php

$request = explode('/', $_SERVER['REQUEST_URI']);
$script = explode('/',$_SERVER['SCRIPT_NAME']);

$categorias=array_values(array_diff($request,$script));


// hacemos un mini debug para ver como funciona internamente
echo '<pre>';
print_r($categorias);
echo "<hr />";

echo "*** lo que viene del server:<br />";
var_dump($_SERVER);
echo "*** la ruta filtrada por htaccess:<br />";
var_dump($request);
echo "*** la ruta de este script en el servidor:<br />";
var_dump($script);

echo '</pre>';

?>

In the array $categorias you have the parts of the url that you must then transform into numbers to load the content

for your example (I write it literally but surely you will make it dynamic by consulting a table of sections and categories)

$seccion=0;
$categoria=0;

if ('oficinas'==$categorias[0]) $seccion=24;
if ('logistica'==$categorias[1]) $categoria=56;

echo "index.php?seccion=$seccion&categoria=$categoria<br />"
    
answered by 03.04.2018 в 02:11