用 htaccess 替换 seo url 中的参数
Replacement of parameters in seo url with htaccess
我的url:
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=&ilce=&marka=&model= [L,QSA]
如果所有参数都存在则有效。但如果 url.
中没有 ilce 或 model 参数则不起作用
所以我希望它在这些情况下也能正常工作
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 应该如何处理?
根据您展示的示例,请您尝试以下操作。请确保在测试您的 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=&marka=&model= [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=&marka= [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=&marka=&model= [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=&marka=&model= [NC,L]
##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/([^-]*)-([^-]*)/?$ ara?il=&marka= [NC,L]
我的url:
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=&ilce=&marka=&model= [L,QSA]
如果所有参数都存在则有效。但如果 url.
中没有 ilce 或 model 参数则不起作用所以我希望它在这些情况下也能正常工作
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 应该如何处理?
根据您展示的示例,请您尝试以下操作。请确保在测试您的 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=&marka=&model= [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=&marka= [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=&marka=&model= [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=&marka=&model= [NC,L]
##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/([^-]*)-([^-]*)/?$ ara?il=&marka= [NC,L]