使用htaccess重写规则将URL段连字符替换为空格 [英] Replace URL segment hyphens with spaces using htaccess rewrite rules

查看:50
本文介绍了使用htaccess重写规则将URL段连字符替换为空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个友好的网址,如下所示:

I have a friendly URL as follows:

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

这是我的htaccess重写规则:

And here's my htaccess rewrite rule:

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

和SQL查询:

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

是否可以使用htaccess和重写规则将连字符替换为空格?因此,友好的网址将被映射到:

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

但是,在我的htaccess中,语义顺序令人困惑,但是在rsrs中有效

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

推荐答案

尝试一下:

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>

它映射:

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

收件人:

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

用空格字符替换所有连字符.

Replacing all the hyphens with space characters.

如果还需要重写另一个段,则只需在最后一个 RewriteRule 中匹配该段.第一个替换整个请求URI中的连字符.

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]


更新2

关于您的问题的最新更新,我必须说,有一种更好和更清洁的方法可以实现相同的目的效果.


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.

据我所知,除了一条将请求映射到 tudo-imovel.php 文件的重写规则之外,所有其他请求都被映射到 busca-cidade.php .您可以组合所有条件并仅通过2条重写规则(而不是6条)实现相同的条件.

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.

但是,这种方法可能会有一些副作用.它始终设置所有查询字符串参数( locacao_venda cidade tipo_mae 等),即使某些参数可能为空.那是由于多个规则的组合.

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.

但是,据我所知,这没有什么严重的问题.您唯一需要注意的就是在 $ _ GET 变量上使用 empty()(而不是检查是否已使用 isset()).

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
#

您看到我只使用一种重写规则来替换那些连字符.您在每个重写规则中都重复了该操作.这是不必要的,因为它以递归方式替换所有连字符,然后使用其他重写规则.看到那些 RewriteCond%{REQUEST_URI}!\-作为防护,直到替换所有连字符为止.

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.

不确定是否有一种简单的方法来删除空查询字符串参数.由于您没有使用 R 标志,并且所有这些操作都是在后台进行的,因此没有任何烦人的事情.

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.

这篇关于使用htaccess重写规则将URL段连字符替换为空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆