The Little Guy Posted August 30, 2010 Share Posted August 30, 2010 I have the following: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^category/(([A-Z]|[0-9]).+?)/([0-9].+)$ /story.php?category=$1&storyid=$2 [L,QSA] http://site.com/category/Sports/1212112 storyid is the number at the end of the url, how come when I echo out storyid, it is "S" and not "1212112" (or what ever the number happens to be)? Quote Link to comment https://forums.phpfreaks.com/topic/212090-get-last-part-of-url/ Share on other sites More sharing options...
DavidAM Posted August 30, 2010 Share Posted August 30, 2010 I'm no RegExpert but ... To determine the backreference number, you count the openning parenthesis from the left. ^category/(([A-Z]|[0-9]).+?)/([0-9].+)$ Your first backreference is capturing everything between the first set of slashes - "Sports" Your second backreference is capturing the FIRST character after the FIRST slash - "S" Your third backreference is capturing everything after the last slash - "1212112" You don't really need the second capture. The way I read that first group it says: (([A-Z]|[0-9]).+?) First character is a capital letter or a digit followed by any character one or more times wouldn't this work the same? ([A-Z0-9].+?) without capturing the initial character? Also, the second expression: ([0-9].+) inidicates the string needs to start with a digit, but can have any character following it (1 or more times) - were you trying for just digits? If so, I think you need to remove the dot: ([0-9]+) If I'm wrong, someone please correct me. I'm trying to understand these things, but it seems to be a black-art. Quote Link to comment https://forums.phpfreaks.com/topic/212090-get-last-part-of-url/#findComment-1105296 Share on other sites More sharing options...
The Little Guy Posted August 30, 2010 Author Share Posted August 30, 2010 Ahh...! I never thought about that being the third reference! That was exactly the problem! but modifying it to this: ([A-Z0-9].+?) gives this: Sports/1212112 here is the final line: RewriteRule ^category/(([A-Z]|[0-9]).+?)/([0-9]+)$ /story.php?category=$1&storyid=$3 [L,QSA] Thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/212090-get-last-part-of-url/#findComment-1105301 Share on other sites More sharing options...
DavidAM Posted August 30, 2010 Share Posted August 30, 2010 Hmm, that's strange. Maybe the rewrite engine does not support using the "?" to make the + not-greedy. You could try: ([A-Z0-9][^/]+) which says anything that is NOT a slash. Quote Link to comment https://forums.phpfreaks.com/topic/212090-get-last-part-of-url/#findComment-1105306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.