1

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.

Igor Carmagna
  • 895
  • 10
  • 29

3 Answers3

2

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.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
nakashu
  • 988
  • 11
  • 19
  • Great answer!! I arrived at the same conclusions. Look at the comments I wrote yesterday to A.L answer. I also looked into SEO and slugs. This answer can help other users (avoiding extensive researches in the dark). Thank you for your time – Igor Carmagna Oct 18 '15 at 19:37
1

You mixed two different concepts:

Links

The two following links carry the same parameters, news and comment ids:

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

A browser opening these URLs will not scroll the page.

Links with anchor

This following link have only one parameter, news id, and it uses an anchor:

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

A browser opening this URL will scroll to the anchor.

So it depends of your need, if you want that the visitors' browsers scroll to the comment, use an anchor (it's also possible with Javascript).

Here are valid anchors:

<div id="comment-42">...</div>
or
<p><a href="#" name="comment-42">...</a></p>

Here is a link leading to this answer: https://stackoverflow.com/questions/33176341/php-url-structure-to-link-specific-comment/33179630#33179630

See also #! URLs.

Community
  • 1
  • 1
A.L
  • 9,074
  • 9
  • 55
  • 87
  • Thank you, but my problem is that in the "single news" page the comments are paginated (with ajax) so a comment-url needs to pass a comment-id to a controller which will do some math and display the news with the comment pagination at the right page number. How do I do that (avoiding duplicated urls maybe)? Should I use an ajax call to load the comments even the first time (when the single news page is rendered)? – Igor Carmagna Oct 17 '15 at 06:09
  • Clarification: a possible solution would be using a /news/{news_id} route to fetch the news content. Then INSIDE THE TWIG TEMPLATE I will check if the url has an anchor parameter. If so, I retrieve the comment_id from the anchor with javascript and make an ajax call to fetch the right comment page number. From this point onward it's all as usual... fetch the comment page content and display it. Could this work? – Igor Carmagna Oct 17 '15 at 06:17
  • With the above solution, I would even avoid duplicated links right? Is this solution considered "best practice"? – Igor Carmagna Oct 17 '15 at 06:21
  • @IgorCarmagna: You can add the page number in the URL to display the comments page containing the comment. – A.L Oct 17 '15 at 19:51
-1

I believe the better way is using #comment-{comment_id} because the duplicate URLs problem that the other 2 URLs may cause.

Felipe Umpierre
  • 188
  • 2
  • 12
  • Here on SO they use hashtags # as well. Now I need to discover how to retrieve/parse inside a controller the information provided with a hashtag. Any suggestion? – Igor Carmagna Oct 16 '15 at 17:47
  • @IgorCarmagna hashtags? These are [anchors](http://www.w3.org/TR/html401/struct/links.html#h-12.2.1). – A.L Oct 16 '15 at 21:05
  • 1
    not sure how #comment-{comment_id} solves the duplicate problem ? or how the others two cause "duplicate problems" for that matter – wonde Oct 16 '15 at 21:11
  • @wonde: using the `comment_id` in the URL will create one URL for each comment, leading to the same `news_id`, I think that's duplicated content. – A.L Oct 16 '15 at 23:28
  • @A.L that's correct! Google will considerate the content duplicated because have two urls with the same content. Using the anchor to link the comment in the url, this extra information will not be considered by Google. – Felipe Umpierre Oct 17 '15 at 03:51