How can I simplify URLs for a website?


To access a particular area of the site, I have to indicate one to three parameters in the URL:

Normal URL:

# aceder a um módulo

# aceder a um sub-módulo:

# Aceder a um conteúdo específico no sub-módulo:

Through htaccess, I am trying to allow access to specific modules, sub-modules and content as follows:

# aceder a um módulo

# aceder a um sub-módulo:

# aceder a um conteúdo específico no sub-módulo:

So far I have the following:

The code below allows me to access the module, but I have to repeat it for each existing module, and still have submodules and specific contents missing:

# Rewrite the url
<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteCond $0 ^john/
  RewriteRule ^([^/]*)/([^/]*)$ /index.php?mod=john [NC,L]

  RewriteCond $0 ^jane/
  RewriteRule ^([^/]*)/([^/]*)$ /index.php?mod=jane [NC,L]



How do I get through .htaccess to read the addresses so that they can be used in both ways on top given the three possible parameters?

asked by anonymous 28.12.2013 / 10:37

3 answers


Method 1: PHP

See an example commented:

# Se o mod_rewrite estiver ativo
<IfModule mod_rewrite.c>

# Ativa a reescrita
RewriteEngine On

# Manuseia as requisições...
 # se a URL não descreve um arquivo ou diretorio existeste
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

 # Então sera redirecionado para index.php
RewriteRule ^ index.php [L]

After using this .htaccess file you can access the URL that the user requested using the variable $_SERVER["REQUEST_URI"]

   $url = explode('/', $_SERVER["REQUEST_URI"]);
   $i = 0;
   foreach($url as $u){
       if($i == 0)
           $module = $u;
       elseif($i == 1)
           $subModule = $u;
       elseif($i % 2 == 0)
           $paramName[] = $u;
           $param[] = $u;

   require_once($module . '/' . $submodule . '.php');

I do not quite understand what your module structure and submodules will look like, but from that code you can treat to include the file correctly and still receive all the parameters.

Method 2: .htaccess (mod_rewrite)

You can use the parameters as follows:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^/]+)/([^/]+)
RewriteRule .*  [L]

Or even more elegantly

// Quando apenas um parametro for passado
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?modulo=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?modulo=$1

// Quando houver um segundo
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?modulo=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?modulo=$1&id=$2

28.12.2013 / 11:50

So I understand you're trying to handle all the rules in .htaccess, I find this difficult because in your example it seems like it will work with modules and submodules, but this is not a rigid rule, sometimes only with modules.

The ideal would be to handle the .htaccess redirecting to index.php, as already described in the hernandes response. However, remember that you will have to pass actions and parameters also through the url.

One way to help productivity and quality assurance is to use a framework that already has well established routing processes and enables you to focus on developing the business rules of your system, not on infrastructure.

One suggestion is to use a microframework, some options are:

  • Lumen: link - based on Laravel
  • Silex: link - based on symfony
28.12.2013 / 15:46

I recommend using Routers to work with simplifying urls.

Here is an example created by Brazilians link

There is a very easy router to use.

30.12.2013 / 13:44