Optimizing the URLs using slugs
Posted by admin on December 8, 2016
If you are using the URLs such as http://domainname/index.php?pid=12&uid=821 they do serve the purpose of opening the correct page however they do not provide any information to the user as to what page they are going to see and what they should expect on this page. Also, as we all know the URLs are also very useful for search engine optimizations, so unless the URL conveys complete information it is not of much use.
So in the above case let's assume that pid represents the page (for example user's profile page) that needs to be opened and uid represents the user's id whose information will be displayed on this page, if we change the URL to something like http://domainname/userprofile/firstname-lastname
(Be aware google treats dash and underscore differently in the URL and dashes are better) then it conveys a lot of meaning to the end user and also is meaningful for search engines. However one point to note above is that a URL needs to be unique and in the above URL there is no element that makes it unique. So we need to insert another element that is unique in the above url. Generally each of the resource has an id associated with it, so that can be used to make a unique url something like http://domainname/userprofile/821/firstname-lastname
Now to achieve this change in URL all you have to do is map pid=12 with userprofile using htaccess rules and use the unique id to render the page. Also, you would want to create a utility function that will generate the user profile URLs. Within this function you can encapsulate the logic of URL creation, whether it is as simple as concatenating two fields or using a new table column as slug.