well i can give you some pseudo code if you want
take the string in question and look for the "/" symbol
if it exist then count the number of letters to that symbol
put it into its own variable
if that variable exists
trucate with variable in expression
else
if "/" does not exist then compare them to your urls in your database