I implemented short-urls for categories and some other links back in 2009. Used Mod-Rewrite in .htaccess to redirect visitors based on category names. But I did not use it for content (recipes, videos, meals etc.) links due to its dynamic nature. Basically I didn’t spend enough time researching and reading up on this feature.
Later on I learned how the Mod-Rewrite can be used to read values from query string and pass over to the redirected URL. I implemented this by adding “rtitle” in query string to pass the recipe title. Modified the recipe page to read “rtitle” value and use it to load corresponding recipe. Incase there is nothing found… the user was redirected to the search result page with the title as search entry. This worked pretty well and I generated my site-map.xml to submit to Google and Yahoo. But then I started seeing some issues. Three issues to be precise.
First… Recipes in my database are listed by unique IDs and not unique Titles. Meaning… there were (many) cases where more than one recipe had same title. This was not a big issue because in this case I was redirecting users to the search page where they will see all recipes under the same title.
Second… Special characters in title like ()!-? etc. were not really URLencoded. This was causing some recipes to not work also because the sitemap.xml was breaking. I cleaned all titles and removed special characters but the other admins and recipe experts continued to add these characters here and there.
Third… Name change!! Admins and Experts had to be forbidden from changing the recipe title. Why? because if I have submitted www.pakirecipes.com/recipe/Haleem in sitemap and now it is renamed to Easy Haleem… it will not work.
After lots of research and lots of thinking I came up with a solution that addresses all of the above three issues. Here is how…
I made Recipe Title a unique column in MySQL database. Therefore no two recipes can have same title. I produced a list of all recipes that had duplicate names and had the Jr. Admins go through it. They slightly changed names so I can apply Unique constraint.
Instead of using the title of the recipe I made a new table ‘Permalinks’ to store the permalinks. This table has 3 columns. Recipe ID, Permalink and create date (time-stamp). I populated this table with stripped recipe titles and their IDs. I am using hyphens (-) as separators… not underscores and not + sign. Using a PHP function I removed all special characters and replaced spaces with hyphen from the recipe titles. This solves the special character problem (no. 2 above).
Lastly… since the sitemap contains the permalink and not the actual title… the renaming of title changes nothing. This solves the third problem.
What if Haleem becomes Nehari…. the Haleem permalink will take user to Nehari??? you must ask!!!
A brilliant question. Brilliant because I asked that too, first 🙂
That is where the timestamp (3rd) column comes in play. Renaming recipe title creates a new permalink and inserts in the Permalink table with new timestamp. So if Haleem was 23 and now Haleem is renamed to Nehari… the table contains 23:Haleem and 23:Nehari with Nehari having latest date to it. Now if someone visits Nehari they will get to Nehari (23). If they visit Haleem (from already submitted and popular link) they will get to Nehari… unless there is a new recipe with title Haleem. This technique covers all scenarios and makes everyone happy. I just had to update the Recipe Adding/Editing page to make sure no two recipes had the same title and Recipe ID, Permalink is unique.
So far this have been working as desired. Have submitted my sitemap to Google and Yahoo. Only the newest of two (or more).