URL friendly with various paramteros in htaccess

Asked

Viewed 304 times

2

i see that some sites like olx, search among others, when sent several parameters in the url is separated these parameters by / or -.

For example: http://sc.olx.com.br/norte-de-santa-catarina/imoveis/casas-a-partir-de-450-00-reais-118704544

I need help eliminating ? and & and separating by /.

Example:

site.com.br/imovel? id=3366

Leave that way:

site.com.br/imovel/3366

My htaccess is configured this way but not working:

RewriteEngine On

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

RewriteRule ^([a-z0-9\-]+)$ index.php?pagina=$1 [QSA]
RewriteRule ^exclusive/imovel/(\d+)$ imovel?id=$1 [QSA]
RewriteRule ^exclusive/lista/(\d+)$ lista?id=$1 [QSA]

I’m using php and the function to manipulate the url like this:

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');
        }
  • I believe your problem is not in htaccess. The problem is how your application interprets the named urls.

  • Igor, your application was developed in what language? Are you using any framework?

  • I’m using php, but no Framework.

  • I edited the question.

1 answer

2

Replace the last three lines with Rewriterule . * index.php

Then manipulate the contents of $_SERVER[ 'REQUEST_URI' ].

  • I edited the question.

  • Jaiber, can you show me an example of using $_SERVER['REQUEST_URI'] to manipulate the url in php?

  • I built a hmvc framerwork that uses the following . htaccess by default: RewriteEngine On

RewriteRule ^public/css/?(.*) style.php
RewriteRule ^jf/public/?(.*) core/public/$1

RewriteCond %{REQUEST_URI} !public
RewriteCond %{REQUEST_URI} !style.php
RewriteCond %{REQUEST_URI} !robots.txt
RewriteCond %{REQUEST_URI} !sitemap.xml

RewriteRule .* index.php

  • In PHP, use ... <?php&#xA;&#xA;$urlBase = dirname( $_SERVER[ 'PHP_SELF' ] );&#xA;if ( substr( $urlBase, -1 ) !== '/' ) {&#xA; $urlBase .= '/';&#xA;}&#xA;&#xA;$controller = 'webapp';&#xA;$action = 'home';&#xA;$params = array();&#xA;&#xA;$urlRequest = substr( $_SERVER[ 'REQUEST_URI' ], strlen( $urlBase ) );

  • if ( $urlRequest ) {&#xA; $requestExploded = explode( '/', $urlRequest );&#xA; $controller = array_shift( $requestExploded );&#xA; &#xA; count( $requestExploded ) && $action = array_shift( $requestExploded );&#xA;&#xA; if ( $qtdParams = count( $requestExploded ) ) {&#xA; for ( $i = 0; $i < $qtdParams; $i += 2 ) {&#xA; $key = $requestExploded[ $i ];&#xA; $value = $i + 1 < $qtdParams ? $requestExploded[ $i + 1 ] : null;&#xA; $params[ $key ] = $value;&#xA; }&#xA; }&#xA;}&#xA;

  • define( 'CONTROLLER', $controller );&#xA;define( 'ACTION', $action );&#xA;define( 'PARAMS', implode( '/', $params ) );&#xA;&#xA;define( 'URL_BASE', $urlBase );&#xA;define( 'URL_REQUEST', $urlRequest );&#xA; Ready

  • Thank you, I’ll test the code.

  • Blz, I hope it helps.

Show 3 more comments

Browser other questions tagged

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