Php - URL structure to link a specific comment

advertisements

Context: I am building the blog section of a website in Symfony2.

Question: Which is the best way to link a specific comment in a news? How should I define the route structure?

Examples:

Single News Url:

example.com/news/{news_id}

Single News + Comment Url:

example.com/news/{news_id}/comment/{comment_id}
or
example.com/news/{news_id}#comment-{comment_id}
or
example.com/news/{news_id}?comment={comment_id}

These are just some suggestions...

VERY IMPORTANT:
I need to use both the news_id and the comment_id inside a controller. They need to be retrievable/available.


Structure of the suggested links will have different outcomes, and your comment_id variable wont be available for your script in all cases.

Something important for news page they affect SEO differently.

first 2 variants of the url.

example.com/news/{news_id}/comment/{comment_id}

example.com/news/{news_id}?comment={comment_id}

  • commment_id WILL be available in your script. Symfony will pass to your controller or you will be able to get it from the Request object (or from $_GET variable - but don't do this)
  • browser WILL NOT
  • don't worry you WON'T have duplicate content if you don't create both routes for the same page.
  • from SEO point of view you are creating a separate page for each comment (I know you are not), that's the way what google would except from that url structure. To avoid duplicate content just add canonical link element to HEAD to point to the root url <link rel="canonical" href="example.com/news/{news_id}" />

hashtag url variant

example.com/news/{news_id}#comment-{comment_id}

  • comment_id WILL NOT be available in your script. Everything after # is handler directly by browser, and it WILL NOT send it to the server at all. The comment_id value WILL still be available by javascript (this is how stackoverflow does it)
  • browser WILL try to move(scroll) to a portion of the html, where the Key after # is used as ID.. eg. <div id="comment_id-123">. If you don't have element with the ID in your markup, it will stay on top.

solution

Based on assumption that you don't want separate page for each comment, and you only need the comment_id for pagination of the comments.

Right solution would be to use the # variant of URL.

  • load the page with just the news_id
  • after page load, do an ajax call with the comment_id parameter for comments, or for 1st page if there is no parameter.
  • change the comment section with returned information about pages etc.
  • add loader images, there so user will know whats happening as this increases UX.

more SEO suitable alternative

If you want better SEO, more suitable url would be not with ids but with slugs, and also without unnecessary words. I personally suggest this:

example.com/n/{news_slug}#{comment_title_slug}-{comment_id}
// would become something like1
example.com/n/answer-to-life-is-awesome#yeah-it-was-a-great-book-5435335435

If you can handle the parsing, and db querying, it could be also without the /n prefix.