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 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab