: How can I programmatically redirect to new pages when users change content and the URL slug changes? I have a PHP website, that has content posted by the administrator, and content posted by
I have a PHP website, that has content posted by the administrator, and content posted by loggedin users. I use a content slug in URLs. If a user changes his post the corresponding slug may changes and the consequently the URL. Whenever an old page is requested I need to redirect to the new one.
This is the method I'm thinking of implementing: create a mysql table containing old URLs and the corresponding new URLs and then whenever a logged in user changes their post, I will programmatically update that table. When requesting a non-existant page, I would first check that table to see if that link exists there to do a redirect 301 to the new URL. Otherwise i will return 404.
Would that be a good approach?
More posts by @Carla537
1 Comments
Sorted by latest first Latest Oldest Best
Yes, you could do something like that. And you would need to do something like that if the "slug" is the only bit of info that uniquely identifies the page. However, is the "slug" really the unique thing that identifies a page? (What if a slug matches an old page or an existing page?)
If you have a short id that uniquely identifies the page (that never changes) and you include this in the URL (like what stackexchange.com does, eg. 108487 is the id of this question page) then you only need to check that the slug is not the current slug before issuing a redirect to the canonical page. Obviously this doesn't use 404s, the unique id must be present in the URL for it to resolve at all. So it would be preferable to include this unique id near the start of the URL. If the slug gets cut-off (when a link is shared etc.) then it doesn't matter, if the short id is still intact.
Just for kicks, I notice that Squarespace, that doesn't use a unique id in user URLs, also does not issue redirects automatically from old to new page URLs. From the help doc on Changing URL slugs:
Traffic won't automatically redirect to a new URL if you change it. Navigating to the old URL will display a 404 Page Not Found error. To prevent this, create a 301 redirect after changing a URL slug.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.