Additional GET parameters does not work with htaccess [duplicate]

3

I'm developing a dynamic website and dynamic url pages, my problem is as follows:

When I put in the link the id to view the details of the property <a href="/imovel?id<echo $row['id_imovel']>"> , the property.php file does not receive the id that was sent via get.

In the url, when it receives property includes imovel.php, but when it is immovable? id = 3174 for example is not made the inclusion.

HTACCESS

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?pagina=$1

PHP:

function getHome(){
        $url = $_GET['pagina'];
        $url = explode('/', $url);
        $url[0] = ($url[0] == NULL ? 'content' : $url[0]);


        if(file_exists('tpl/'.$url[0].'.php')){
            require_once('tpl/'.$url[0].'.php');
        }elseif(file_exists('/tpl/'.$url[0].'/'.$url[1].'.php')){
            require_once('tpl/'.$url[0].'/'.$url[1].'.php');
        }elseif(file_exists('/tpl/'.$url[0].'/'.$url[1].'/'.$url[2].'.php')){
            require_once('tpl/'.$url[0].'/'.$url[1].'/'.$url[2].'.php');
        }else{
            require_once('tpl/404.php');
        }

How to solve?

    
asked by anonymous 03.08.2015 / 21:06

1 answer

1

This is because the QSA flag is missing

You should do this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?pagina=$1 [QSA]

If you do not want to "hide" the ID too, you can use it in this way (with RegEX):

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z0-9\-]+)$ index.php?pagina=$1 [QSA] #Outras páginas ou página sem ID
RewriteRule ^([a-z0-9\-]+)/(\d+)$ index.php?pagina=$1&id=$2 [QSA] #Qualquer página com ID

If you want to use id= only on the property page, then use:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z0-9\-]+)$ index.php?pagina=$1 [QSA]
RewriteRule ^imovel/(\d+)$ index.php?pagina=imovel&id=$1 [QSA]
    
03.08.2015 / 21:10