Help with .htaccess - Url Friendly


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

I have for example the traditional link:

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:

Now I also have this URL:

Where my section is: offices and category is

I try to make it my URL:

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") {
}else {
  echo "error 404";

My link looks like this:

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

And I would like it to be like this:

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

2 answers


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

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)


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)


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


// hacemos un mini debug para ver como funciona internamente
echo '<pre>';
echo "<hr />";

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

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)


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