Additional GET parameters does not work with htaccess

Asked

Viewed 886 times

3

I am developing a dynamic web site m pages and url friendly, my problem is the following:

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

In the url, when immovable it includes immovable.php, but when immovable? id=3174 for example not included.

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?

1 answer

2

This is why the flag is missing QSA

You must do so:

RewriteEngine On

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

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

If you don’t want to "hide" the ID too, you can use it 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 still 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]
  • Solved buddy! Thanks! How can I hide the id, leaving immovable/3174 for example?

  • @Igorsilva edited the answer, see if it helps.

  • Insert the option in my htaccess to any page with ID and it didn’t work. Do I have to handle the id in the getHome function of the php code above? This link goes to immovable page with id: <a href="<? php echo DIR? >/immovel? id=<? php echo $id;?>">.

  • No @Igorsilva I passed by &id=$1 then just use $_GET['id'].

  • Would that be: <a href="<? php echo DIR? >/imovel/<? php echo $id;?>">

  • @Igorsilva said that :) because the index.php?pagina=imovel&id=$1 [QSA] already interprets the id=, then the address should be something like http://exemplo/imovel/10001 and when to use $_GET['id'] will return 10001 for example

  • Returned Not Found, the end of my url is this:www.site.com/exclusive/immovel/3239

  • @Igorsilva the folder exclusive is there? note that I used ^imovel/(\d+)$ and not ^exclusive/imovel/(\d+)$. You are changing the url pattern, there is no way mod_rewrite understands such a change if you do not edit . htaccess to it.

  • My site n is in the root, its url is like this: site.com/exlcusive/immovel/ID, I will insert the exclusive/immovel to see if it works.

  • @Igorsilva You mean that exlcusive is a real folder?

  • Yes, you can post the site url here?

  • @Igorsilva You can! Only one thing answers the . htaccess is in which folder?

  • This is the website: http://himoveis.com/exclusive/ htaccess is in the exclusive folder.

  • @Igorsilva we’ll talk tomorrow :)

  • Vlw! Thank you very much!

Show 10 more comments

Browser other questions tagged

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