Replace URL segment hyphens with spaces using htaccess rewrite rules

杀马特。学长 韩版系。学妹 提交于 2021-02-11 06:19:44

问题


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:


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.



来源:https://stackoverflow.com/questions/41427077/replace-url-segment-hyphens-with-spaces-using-htaccess-rewrite-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!