Resolution
Remove the ?
before the m
and use variable $1
:
RewriteRule ^film/([a-z0-9\-]+)/?$ index.php?p=filmes_v&m=$1 [NC]
The htaccess
will be as follows:
<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 -
, because they are used in the syntax of the expression, as an example [a-z]
indicating of a
until z
. - @Guilhermenascimento
Links and Scripts
Using URL friendly you accurate can use absolute paths or just reference the files indicating a /
at the beginning of the way:
<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 -->
<script src="/js/jquery.min.js"></script>
Relative Paths
Heed: Relative paths has to be initiated with /
which indicates the site root, because if the current page has an address similar to http://meudominio.com/contato
and the user click on a link <a href="sobre">Sobre</a>
he will end up at the address http://meudominio.com/contato/sobre
.
A very interesting resource to use with relative paths is the RewriteBase
. For example imagine that you have a blog and it’s 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/
then 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 in the htaccess
of Blog is defined the RewriteBase
for blog
, will no longer need the /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 decide to change the address http://meusite.com/blog/
for http://blog.meusite.com
just need to change the RewriteBase
for /
or remove it:
RewriteBase /
So you don’t need to change all the links removing the one /blog
of Urls. :)
You can also have this feature through tag base
. But I prefer the htaccess
.
Other means
A very interesting way to make friendly Urls is the way I call it Dynamic Friendly Urls, the implementation would be so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?uri=$1
</IfModule>
This way you can treat the URI in PHP to receive as many variables as you want, example:
$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 http://meudominio/catalogo/categoria/eletrodomestico/preco/100~500/voltagem/220
and you would have the following structure in the variable $vars
:
page => catalogo
categoria => eletrodomestico
preco => 100~500
voltagem => 220
So the first argument is always the page and the following are any parameter you want, in any order and should always be
sent in pairs chave/valor
. This form is quite useful for making filters.
Model MVC
Using this concept it is simple to apply a MVC model that would be:
controller/action/demais/parametros
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;
}
}
}
Thus 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'
)
);
Change the line:
RewriteRule ^film/([a-z0-9-]+)/?$ index.php?p=filmes_v&?m=$1 [NC]
, about CSS would be better to post as you are doing.– Papa Charlie
Thank you @Papacharlie, the $2 was my own distraction.
– xdam98
@Kaduamaral, that was my problem, I managed to solve, thank you.
– xdam98
By the way, the CSS problem from this I think I can solve :)
– xdam98