1

I have a friendly URL as follows:

www.dominio.com.br/cidade/sao-paulo

And here's my htaccess rewrite rule:

RewriteRule ^cidade/(.*)$ busca-cidade.php?cidade=$1

And the SQL query:

Select * from cidades where cidade = 'sao-paulo'

Is it possible to replace the hyphens with spaces using htaccess and rewrite rules? So the friendly URL will be mapped to:

www.dominio.com.br/busca-cidade.php?cidade=sao%20paulo

This finished my code, was great, would give to optimize.

####################################TESTE
RewriteRule ^teste?$ busca-cidade.php?locacao_venda=L
#(1) http://www.imobiliariaemaximovel.com.br/teste 

#GET TIPO IMOVEL
RewriteRule ^(teste/[^\-]*)[\-]+(.*)([^\-]*)([^\-]*)$ $1\ $2 [DPI]
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^teste/([^\-]*)/([^\-]*)/([^\-]*)?$ /busca-cidade.php?locacao_venda=L&cidade=$1&tipo_mae=$2&tipo_imovel=$3 [L,QSA]
#(4) imobiliariaemaximovel.com.br/teste/Sorocaba/Apartamentos/Apto-Padrao

#GETREFERENCIA
RewriteRule ^(teste/[^\-]*)[\-]+(.*)([^\-]*)([^\-]*)([^\-]*)([^\-]*)$ $1\ $2 [DPI]
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^teste/([^\-]*)/([^\-]*)/([^\-]*)/([^\-]*)/([^\-]*)?$ /tudo-imovel.php?codigo=$5 [L,QSA]
#(6) imobiliariaemaximovel.com.br/teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu/16538

#GET BAIRRO
RewriteRule ^(teste/[^\-]*)[\-]+(.*)([^\-]*)([^\-]*)([^\-]*)$ $1\ $2 [DPI]
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^teste/([^\-]*)/([^\-]*)/([^\-]*)/([^\-]*)?$ /busca-cidade.php?locacao_venda=L&cidade=$1&tipo_mae=$2&tipo_imovel=$3&bairro=$4 [L,QSA]
#(5) imobiliariaemaximovel.com.br/teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu

#GET TIPO MAE
RewriteRule ^(teste/[^\-]*)[\-]+(.*)([^\-]*)$ $1\ $2 [DPI]
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^teste/([^\-]*)/([^\-]*)?$ /busca-cidade.php?locacao_venda=L&cidade=$1&tipo_mae=$2 [L,QSA]
#(3) imobiliariaemaximovel.com.br/teste/Sorocaba/Apartamentos

#GET CIDADE LOCAÇÃO
RewriteRule ^(teste/[^\-]*)[\-]+(.*)$ $1\ $2 [DPI]
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^teste/([^\-]+)$ /busca-cidade.php?locacao_venda=L&cidade=$1 [L,QSA]
#(2) imobiliariaemaximovel.com.br/teste/Sorocaba

Semantic order, however, in my htaccess it is confusing, but works rsrs

#(1) /teste 
#(2) /teste/Sorocaba
#(3) /teste/Sorocaba/Apartamentos
#(4) /teste/Sorocaba/Apartamentos/Apto-Padrao
#(5) /teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu
#(6) /teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu/16538
1

1 Answer 1

1

Try this:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on

    # Internal rewrite to recursively replace hyphens with
    # spaces, only on URIs that start with `/cidade`.
    RewriteCond %{REQUEST_URI} ^/cidade
    RewriteRule ^([^\-]*)[\-]+(.*)$ $1\ $2 [DPI]

    # Now rewrite requests to the php file if only there's
    # no hyphen in the request segment after `cidade/`.
    RewriteCond %{REQUEST_URI} !\-
    RewriteRule ^cidade/(.+)$ /busca-cidade.php?cidade=$1 [L]
</IfModule>

It maps:

www.dominio.com.br/cidade/sao-paulo

To:

www.dominio.com.br/busca-cidade.php?cidade=sao%20paulo

Replacing all the hyphens with space characters.


Update 1

In case you need to rewrite another segment as well, you just need to match that segment in the last RewriteRule. The first one replaces hyphen characters throughout the whole request URI.

RewriteRule ^cidade/(.+)/(.+)$ /busca-cidade.php?cidade=$1&bairro=$2 [L]

Update 2

Regarding the latest update to your question, I must say that there's a slightly better and cleaner way to achieve the same effect.

As far as I see, except that one rewrite rule which maps a request to tudo-imovel.php file, all other requests are being mapped to busca-cidade.php. You can combine all the conditions and achieve the same only by 2 rewrite rules, not 6.

However, there might be a slight side-effect to this approach, though; It always sets all the query string params (locacao_venda, cidade, tipo_mae, etc.), even though some might be empty. That's due to the combination of multiple rules together.

But, as far as I see, there's no serious issue with this. The only thing that you need to take care of is to use empty() on your $_GET variables (instead of checking whether it's set or not using isset()).

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_URI} ^/teste
    RewriteRule ^([^\-]*)[\-]+(.*)$ $1\ $2 [DPI]

    RewriteCond %{REQUEST_URI} !\-
    RewriteRule ^teste/(.+/){4}(\d+) /tudo-imovel.php?codigo=$2 [L,QSA]

    RewriteCond %{REQUEST_URI} !\-
    RewriteRule ^teste/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?$ /busca-cidade.php?locacao_venda=L&cidade=$1&tipo_mae=$2&tipo_imovel=$3&bairro=$4 [L,QSA]

    RewriteCond %{QUERY_STRING} ^(.*)=&(.*)
</IfModule>

## Results:
#
# Maps this:
#    /teste
# To:
#    /busca-cidade.php?locacao_venda=L&cidade=&tipo_mae=&tipo_imovel=&bairro=
#
# Maps this:
#    /teste/Sorocaba
# To:
#    /busca-cidade.php?locacao_venda=L&cidade=Sorocaba&tipo_mae=&tipo_imovel=&bairro=
#
# Maps this:
#    /teste/Sorocaba/Apartamentos
# To:
#    /busca-cidade.php?locacao_venda=L&cidade=Sorocaba&tipo_mae=Apartamentos&tipo_imovel=&bairro=
#
# Maps this:
#    /teste/Sorocaba/Apartamentos/Apto-Padrao
# To:
#    /busca-cidade.php?locacao_venda=L&cidade=Sorocaba&tipo_mae=Apartamentos&tipo_imovel=Apto%20Padrao&bairro=
#
# Maps this:
#    /teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu
# To:
#    /busca-cidade.php?locacao_venda=L&cidade=Sorocaba&tipo_mae=Apartamentos&tipo_imovel=Apto%20Padrao&bairro=Jardim%20Pacaembu
#
# Maps this:
#    /teste/Sorocaba/Apartamentos/Apto-Padrao/Jardim-Pacaembu/16538
# To:
#    /tudo-imovel.php?codigo=16538
#

You see that I only used one rewrite rule to replace those hyphens. You were repeating it for each of your rewrite rules. It's unecessary, as it recursively replaces all of the hyphens and then goes to other rewrite rules. See those RewriteCond %{REQUEST_URI} !\- as guards until all hyphens are replaced.

Not sure if there's an easy way to drop empty query string params. Since you're not utilizing the R flag and all these happens under the hood, there's nothing annoying about it.

Sign up to request clarification or add additional context in comments.

6 Comments

Not correct, this code replace RewriteRule ^cidade/([^\-]+)$ /busca-cidade.php?cidade=$1 [L] to RewriteRule ^cidade/([^\-]+)$ /busca cidade.php?cidade=$1 [L] Return not fund I like this only path cidade no everone domain Regards
Try it now; I added an extra condition to only apply the rules on URIs starting with /cidade. And please bear in mind that the DPI flag is available in Apache version 2.2.12 and later.
One more problem, so, with city without hyphens ex: cidade/orlando, fail. Kurole
Look at my changes
@kurole: Well done! It's fine as long as it works for you. However, I updated the answer with a better solution, in case that you want a slightly cleaner solution to all those rewrites.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.