4

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?

  • 2
    Do you have a numeric ID in the URL? This site solves that problem by putting the question ID in the URL near the beginning. If the URL slug changes, it only needs to look at the question ID. – Stephen Ostermiller Aug 9 '17 at 9:16
  • thank you Stephen, that solution sounds good, am going with that. – ZeSoft Aug 9 '17 at 9:39
4

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.

  • 2
    Thank you DocRoot for your answer, am going to add the id to the url, it's practical. – ZeSoft Aug 9 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.