How to generate unique urls and pages with template?

Tags: , ,



I am building a petition site where you can create/join petitions.
I have made a page where there is table filled with many pre-created petition titles and I want to make that when people click on any petiton title, they would get to another page that gets data from my database and fills it by a template(title, description and etc) and generates a unique url with the petition title in it so that people can share it with their friends.

Below is linked a site that represents 100% of that how I want it to work.

For an example this site has this url https://www.change.org/petitions , but when you click on any petition it redirects you to a specific to this petition url, for an example
https://www.change.org/p/count-every-vote-in-pennsylvania?source_location=petitions_browse or https://www.change.org/p/now-we-want-news-outlets-to-delay-declaring-election-results-until-they-re-final?source_location=petitions_browse .

I am new to coding and I can’t find anything about such action, so any guidance on what is this called and etc. is very welcomed.

Answer

  • take the title of the petition
  • remove any non-url safe characters, like / or ?
  • replace any whitespaces with -
  • check if the result is already present in your database
    • no: return the title and save it
    • yes: append a random number to it, save and return


Source: stackoverflow