【问题标题】:Replace URL segment hyphens with spaces using htaccess rewrite rules使用 htaccess 重写规则将 URL 段连字符替换为空格
【发布时间】:2017-01-02 12:43:45
【问题描述】:

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

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

这是我的 htaccess 重写规则:

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

以及SQL查询:

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

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

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

这完成了我的代码,很棒,可以优化。

####################################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 有效

#(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

【问题讨论】:

标签: regex apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

试试这个:

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

所有连字符替换为空格字符。


更新 1

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

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

更新 2

关于latest update你的问题,我必须说有一种更好更清洁的方法可以达到同样的效果。

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

不过,这种方法可能会产生轻微的副作用;它总是设置所有查询字符串参数(locacao_vendacidadetipo_mae 等),即使有些可能是空的。这是由于多个规则组合在一起造成的。

但是,据我所知,这并没有什么严重的问题。您唯一需要注意的是在您的$_GET 变量上使用empty()(而不是使用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} !\- 视为警卫,直到所有连字符都被替换。

不确定是否有一种简单的方法可以删除空查询字符串参数。由于您没有使用 R 标志并且所有这些都发生在幕后,因此没有什么烦人的。

【讨论】:

  • 不正确,此代码将 RewriteRule ^cidade/([^\-]+)$ /busca-cidade.php?cidade=$1 [L] 替换为 RewriteRule ^cidade/([^\- ]+)$ /busca cidade.php?cidade=$1 [L] 回报不是基金 我喜欢这条唯一路径 cidade noeverone domain 问候
  • 立即尝试;我添加了一个额外的条件,只对以/cidade 开头的 URI 应用规则。请记住,DPI 标志在 Apache 版本 2.2.12 及更高版本中可用。
  • 还有一个问题,所以,没有连字符的城市例如:cidade/orlando,失败。库洛尔
  • 看看我的改动
  • @kurole:干得好!只要它对你有用就可以了。但是,我用一个更好的解决方案更新了答案,以防你想要一个更干净的解决方案来处理所有这些重写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 2013-08-27
  • 1970-01-01
  • 2014-01-09
  • 2014-06-30
  • 2016-08-09
相关资源
最近更新 更多