【问题标题】:Replacement of parameters in seo url with htaccess用 htaccess 替换 seo url 中的参数
【发布时间】:2021-04-25 23:44:56
【问题描述】:

我的网址:

x.com/ara?il=istanbul&ilce=avcilar&marka=opel&model=corsa

x.com/rent/istanbul-avcilar-opel-corsa

在 htaccess 中:

RewriteRule ^rent/([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)$ ara.php?il=$1&ilce=$2&marka=$3&model=$4 [L,QSA]

如果所有参数都存在,它就可以工作。但如果 url 中没有 ilcemodel 参数则不起作用。

所以我希望它也能在这些情况下工作

x.com/ara?il=istanbul&marka=opel&model=corsa 
x.com/rent/istanbul-opel-corsa

x.com/ara?il=istanbul&marka=opel 
x.com/rent/istanbul-opel

x.com/ara?il=istanbul&ilce=avcilar&marka=opel 
x.com/rent/istanbul-avcilar-opel

htaccess 应该怎么做?

【问题讨论】:

  • 能否请您提供将由x.com/ara?il=istanbul&marka=opel&model=corsa 提供的URL 以及将由x.com/ara?il=istanbul&ilce=avcilar&marka=opel 提供的URL。这将使我们更好地理解您的问题,谢谢。
  • 对不起。我编辑添加了

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


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##Rule for rewrite of (x.com/rent/istanbul-opel-corsa) TO (x.com/ara?il=istanbul&marka=opel&model=corsa) url.
RewriteRule ^rent/(istanbul)-(opel)-(corsa)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/(istanbul)-(opel)/?$ ara?il=$1&marka=$2 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-avcilar-opel) TO (x.com/ara?il=istanbul&ilce=avcilar&marka=opel) url.
RewriteRule ^rent/(istanbul)-(avcilar)-(opel)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]


对于通用规则集,请尝试以下操作:

RewriteEngine ON
##Rule for rewrite of (x.com/rent/istanbul-opel-corsa) TO (x.com/ara?il=istanbul&marka=opel&model=corsa) url.
RewriteRule ^rent/([^-]*)-([^-]*)-(.*)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/([^-]*)-([^-]*)/?$ ara?il=$1&marka=$2 [NC,L]

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2013-01-22
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多