Your .htaccess
would look like this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
</IfModule>
With that I define that I will hide the index.php
of the link, and the $_GET['url']
will be worth all the rest of my link after the index
.
After that just manipulate this variable to get the parameters of the link, in your case, as it seems to me that there will always be only one parameter so it is simpler, just take the $_GET['url]
anywhere and ready. An example would be the index.php
thus:
<?php
if(isset($_GET['url']))
echo "<h1>Param: " . $_GET['url'] . "</h1>";
else
echo "<h1>Param Undefined!</h1>";
When you have more than one parameter, you should use a controller to rescue the parameters, I will exemplify with a code from a micro framework that I developed during this vacation. Part of the parameter controller is like this:
// pego os parametros
$url = isset($_GET['url']) ? $_GET['url'] : '';
// apago a variavel do get
unset($_GET['url']);
// se tiver algum parametro
if(!empty($url))
{
// separo os parametros em um array, exemplo meusite.com/categoria/1
// ficaria [0] = 'categoria', [1] = '1'
$params = explode('/', $url);
// arqui enta o MVC, pego meu controller
$_GET['controller'] = isset($params[0]) ? $params[0] : '';
// aqui entra parte do meu framework, verificando se há um sinônimo para esse controller
$alias = Alias::check($_GET['controller']);
// se achou um sinônimo
if($alias != false)
{
// pega o controller e o metodo daquele sinonimo encontrado
$_GET['controller'] = explode('/', $alias)[0];
$_GET['method'] = explode('/', $alias)[1];
}
// se nao achou um sinonimo do link
else
{
// pega o metodo da URL e deleta o mesmo do array
$_GET['method'] = isset($params[1]) ? $params[1] : '';
unset($params[1]);
}
// deleta o controller do array
unset($params[0]);
// pega os demais parametros
$get = array();
foreach ($params as $value)
array_push($get, $value);
$_GET['params'] = $get;
}
I think by showing a more complex example we can understand more about the subject.
Ps.: to make use of Apache-friendly URL, it is necessary to enable the module rewrite
, some hosting already leaves it enabled, others do not, in such cases it is necessary to contact the support request the service. If you are in the localhost
, it can be enabled on WAMP as follows:
Or manually on httpd.conf:
Uncomment or include: (line 109, normally)
#LoadModule rewrite_module modules/mod_rewrite.so
Thus remaining:
LoadModule rewrite_module modules/mod_rewrite.so
Replace all:
AllowOverride None
for:
AllowOverride All
and so it should work..
https://web.archive.org/web/20140220075845/http://www.phpriot.com/Articles/search-engine-urls
– Guerra