Resolution
Remove ?
before m
and use $1
variable:
RewriteRule ^film/([a-z0-9\-]+)/?$ index.php?p=filmes_v&m=$1 [NC]
The htaccess
will look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^film/?$ index.php?p=filmes [NC,L]
RewriteRule ^film/([a-z0-9\-]+)/?$ index.php?p=filmes_v&m=$1 [NC]
</IfModule>
Note that within []
in regular expressions it is recommended to escape -
, since they are used in the syntax of the expression, such as [a-z]
, which indicates a
to z
. - @GreenGirls
Links and Scripts
Using friendly URL you can use absolute paths or just reference the files indicating a /
at the beginning of the path:
<link href="http://meudominio.com/css/meucss.css">
<!-- ou -->
<link href="/css/meucss.css">
<a href="http://meudominio.com/contato">
<!-- ou -->
<a href="/contato">
<script src="http://meudominio.com/js/jquery.min.js"></script><!--ou--><scriptsrc="/js/jquery.min.js"></script>
Relative Paths
Caution: Relative Paths must be started with /
indicating the root of the site, because if the current page has an address similar to http://meudominio.com/contato
user clicking on a <a href="sobre">Sobre</a>
link it will end at the http://meudominio.com/contato/sobre
address.
A very interesting feature to use with relative paths is RewriteBase
. For example imagine that you have a blog and it is structured more or less like this on the server:
/root
| - /www
| | - /blog
| | - /css
| | - /js
And the URL to access this blog is http://meusite.com/blog/
so in all your code you will have to reference your scripts and links using the semi relative URL /blog/
.
<link href="/blog/css/estilos.css" rel="stylesheet" type="text/css">
<script src="/blog/js/scripts.js" type="text/javascript"></script>
<a href="/blog/sobre">Sobre</a>
But if Blog is set to htaccess
to RewriteBase
, you will no longer need blog
in the URL:
RewriteBase /blog/
And your URLs can stay:
<link href="/css/estilos.css" rel="stylesheet" type="text/css">
<script src="/js/scripts.js" type="text/javascript"></script>
<a href="/sobre">Sobre</a>
And so if at some point you resolve to change the address /blog/
to http://meusite.com/blog/
you only need to change http://blog.meusite.com
to RewriteBase
or remove it:
RewriteBase /
So you do not need to change all the links by removing such /
from the URLs.)
You can also have this feature via the tag /blog
. But I prefer not to base
.
Other means
A very interesting way to make friendly URLs is what I call Dynamic Friendly URLs , the implementation would look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?uri=$1
</IfModule>
In this way you can treat the URI in PHP to receive as many variables as you want, eg:
$tmp = !empty($_GET['uri']) ? $_GET['uri'] : 'home'; // Página padrão home
$uri = explode('/', $tmp);
$vars = Array();
if (count($uri) > 1){
$key = 'page';
foreach ($uri as $val) {
if (is_null($key))
$key = $val;
else {
$vars[$key] = $val;
$key = NULL;
}
}
}
In the previous example, you can send the URL htaccess
and you would have the following structure in the variable http://meudominio/catalogo/categoria/eletrodomestico/preco/100~500/voltagem/220
:
page => catalogo
categoria => eletrodomestico
preco => 100~500
voltagem => 220
So the first argument is always the page and the following are any parameters you want, in any order and should always be
sent in double $vars
. This form is very useful for making filters.
MVC model
Using this concept it is simple to apply an MVC model that would be:
controller/action/demais/parametros
As for example:
$tmp = !empty($_GET['uri']) ? $_GET['uri'] : 'home'; // Página padrão home
$uri = explode('/', $tmp);
$vars = Array(
// Controller 'index' caso não tenha parâmetros na URI, caso tenha armaza e remove-o da URI
'controller' => (count($uri) > 0 ? array_shift($uri) : 'index'),
// Action 'index' caso não tenha parâemtros na URI
'action' => (count($uri) > 0 ? array_shift($uri) : 'index'),
// Demais parâmetros
'params' => Array()
);
$key = NULL;
if (count($uri) > 1){
foreach ($uri as $val) {
if (is_null($key))
$key = $val;
else {
$vars['params'][$key] = $val;
$key = NULL;
}
}
}
So by accessing the URL:
www.meudominio.com/sobre/empresa/page/3
We will have the following result:
$vars = Array(
'controller' => 'sobre',
'action' => 'empresa',
'params' => Array(
'page' => '3'
)
);