Jump to content

get last part of url


The Little Guy

Recommended Posts

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)?

Link to comment
https://forums.phpfreaks.com/topic/212090-get-last-part-of-url/
Share on other sites

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.