regex 删除查询字符串并匹配 url 中的剩余部分 [英] regex to remove the querystring and match the remaining segments from the url
问题描述
npinti 帮助我创建了一个正则表达式来删除查询字符串并匹配 url /seattle 中的剩余部分/restaurant/sushi?page=2
:像这样应该产生 3 个组:/(.*?)/(restaurant)/([^?]+).*.
第 1 组是座位,第 2 组是餐厅,第 3 组是寿司.如果在最后一个/之后有一个 ?,正则表达式会丢弃 ? 以及后面的所有内容."
npinti helped me create a regex to remove the querystring and match the remaining segments from the url /seattle/restaurant/sushi?page=2
: "Something like so should yield 3 groups: /(.*?)/(restaurant)/([^?]+).*.
Group 1 being seatthe, group 2 being restaurant and group 3 being sushi. If after the last /there is a ?, the regex discards the ? and everything which follows."
我尝试修改上面的内容以在 url /seattle/restaurant?page=2
上执行相同的技巧,但我无法做到.不知道会不会有af querystring或者querystring的参数.所以我需要上面正则表达式的灵活性,它会匹配并丢弃 ?以及接下来的一切.
I have tried modifying the above to do the same trick on the url /seattle/restaurant?page=2
but I could not get it right. I don't know if there will be af querystring or not or the parameters of the querystring. So I need the flexibility from the regex above which will match and discard the ? and everything which follows.
推荐答案
您的重写规则可能如下所示:
Your rewriterules may look like:
RewriteRule /([^/]+)/restaurant/([^/]+)$ mynewpage.php?group1=$1&group2=$2 [QSA,NC,L]
感谢我在下面提供的链接,您可以搜索QSA
、NC
和L
的含义.
Your may search for what QSA
, NC
, and L
mean thanks to the links I provide below.
很抱歉,您的问题听起来很像我不是很好,所以有人可以帮我完成这项工作吗?".我的意思是,环顾四周,你会得到很多答案,只要把手弄脏.
I'm sorry but your question sound very like "I'm not very good, so can someone do the job for me?". I mean, just look around, you'll get a lot of answer, just get your hands dirty.
如果这还不够:
如果您不是在托管环境中(=如果它是您自己的服务器并且您可以修改虚拟主机,不仅是 .htaccess
文件),尝试使用 RewriteLog
指令:它可以帮助您追踪此类问题:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess
files), try to use the RewriteLog
directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
<小时>
我最喜欢的检查正则表达式的工具:
My favorite tool to check for regexp:
http://www.quanetic.com/Regex(不要忘记选择ereg(POSIX) 而不是 preg(PCRE)!)
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
这篇关于regex 删除查询字符串并匹配 url 中的剩余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!