regex to remove the querystring and match the remaining segments from the url

半城伤御伤魂 提交于 2019-12-24 11:28:28

问题


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."

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.


回答1:


Your rewriterules may look like:

RewriteRule /([^/]+)/restaurant/([^/]+)$ mynewpage.php?group1=$1&group2=$2 [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.


  • Here's the wiki of serverfault.com
  • The howto's htaccess official guide
  • The official mod_rewrite guide

And if that's not enough:

Two hints:

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 (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)




回答2:


This should allow you to match all text prior to the ? and nothing else. It will match everything if no ? is present:

[^?]*

Is that all you need to do? Because /(.*?)/(restaurant)/([^?]+).*. looks like it's designed to do something significantly more complicated.



来源:https://stackoverflow.com/questions/9145069/regex-to-remove-the-querystring-and-match-the-remaining-segments-from-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!