Skip to content

Instantly share code, notes, and snippets.

@BenRogersWPG
Created November 14, 2021 18:11
Show Gist options
  • Save BenRogersWPG/3ce8ba97652234eb2d106158b3e73796 to your computer and use it in GitHub Desktop.
Save BenRogersWPG/3ce8ba97652234eb2d106158b3e73796 to your computer and use it in GitHub Desktop.
Find URL Slug
def slugFinder(urlString):
slashSplit = urlString.split('/')
if len(slashSplit[-1]) < 1:
# If the trailing slash exists, just return the item before that, as the trailing slash contains 0 characters after it
if len(slashSplit[3]) == 0:
return '/'
else:
return slashSplit[-2]
else:
# if no trailing slash, determine if it is the root, by comparing the last item (presumed slug) vs the TLD value. If they are the same, then we are at the root
if slashSplit[-1] == slashSplit[2]:
return '/'
else:
return slashSplit[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment