drol55885602 2015-11-13 20:09
浏览 62
已采纳

如何在Wordpress中将PHP字符串转换为slug

I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/. I am using a custom php query on that page with strings in the URL's like this

Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL

How can I make this url into a slug like this

Bad-Debt-Recovery-in/55555/Chambers-County/AL/

as a rewrite? Any help would be appreciated!

UPDATE: This code is actually what I am using. I also made it simpler and created a third variable named "state". One rewrite is for City and one is for County page:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

</IfModule>

# END WordPress
  • 写回答

2条回答 默认 最新

  • douyuliu9527 2015-11-13 21:41
    关注

    What you are asking for, and seeking to accomplish is called: Converting a request path into a query string.

    Use .htaccess RewriteRule directive to modify the incoming request

    RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]
    

    Each ([^\/]+) captures path elements into a variables $1,$2,$3...

    The ? at the end simply denotes that the last / is optional or else the last match could fail

    the \ simply escape the '/' for literal interpretation

    Add as many ([^\/]+) as needed and capture them in the query

    zipcode=$1&location=$2+$3
    

    The modifiers at the end [QSA,L,NC] are called flags

    • QSA appends the query string to the end of the rewrite if any exist
    • L simply says this is the last rewrite
    • NC means not case sensitive

    This is your final solution:

    RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵