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 echarts如何对X轴进行分组
  • ¥15 维纳滤波器的实验题及计算机实现,求解
  • ¥15 fluent模拟静态气体扩散
  • ¥15 java根据模板,生成word文档,需要带目录
  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 关于cpci总线的几个问题,别用人工智能回答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做