How can I simplify Urls for a website?

Asked

Viewed 1,402 times

47

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
http://www.meusite.com/index.php?mod=john

# aceder a um sub-módulo:
http://www.meusite.com/index.php?mod=john&call=doe

# Aceder a um conteúdo específico no sub-módulo:
http://www.meusite.com/index.php?mod=john&call=doe&id=1

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

# aceder a um módulo
http://www.meusite.com/john

# aceder a um sub-módulo:
http://www.meusite.com/john/doe

# aceder a um conteúdo específico no sub-módulo:
http://www.meusite.com/john/doe/1

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, still missing dealing with sub-modules and specific contents:

# 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]

</IfModule>

Question

How do I get through .htaccess read the addresses so that they can be used in both ways shown above taking into account the three possible parameters?

3 answers

29


Method 1: PHP

See a commented example:

# 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 file . htaccess you can access the URL 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;
       else
           $param[] = $u;
   }

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

I do not understand very well how will be your structure of modules and submodules, more from this code you can handle to include the file correctly and still receive all 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 .* http://site.com/index.php?modulo=%1&usuario=%2  [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

.
.
.
  • In this new method, it will also be possible to deal with the part of the however specific as the example: http://www.meusite.com/john/doe/1

  • Yes @Zuul, just by modifying the Rewritecond %{REQUEST_URI} /([ /]+)/([ /]+) line to catch such a pattern.

  • Still, the first method is more secure, even though the code that treats REQUST_URI is a bit confusing

8

From what I understand you’re trying to treat all the rules in the . htaccess, I find this difficult because in your example it seems that will work with modules and submodules, but this is not a rigid rule, sometimes only with modules.

The ideal would be to treat . htaccess by 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 also ensure quality would be to use a framework that already has well-established routing processes and enables you to focus on developing your system’s business rules, not on infrastructure.

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

  • 3

    Dei voteup because I fully agree, a framework is actually the best option...

  • Unnecessary your vote down, is not an answer that leads to a wrong concept or application. But rather an answer that will help in the routing problem he is trying to solve, because as I said in the answer, the proposed solution did not take into account the mapping of the action to be executed and the parameters to be treated.

5

I recommend using Routers to work with URL simplification.

Here is an example created by Brazilians https://github.com/Respect/Rest

There is a router very easy to use.

Browser other questions tagged

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