I have a forum whereby links to a thread looks like
http://www.website.com/comments.php?topic_id=1
How can I make it look like this
http://www.website.com/1046302/some-link-desc#12154109
so that when such links are given out, the user is taken directly to that particular comment.
I'm particular about the #12154109 . The other part of the URL /1046302/some-link-desc is achieved through .htaccess configuration.
Question Update
What is the best way to get the unique number ? Do I use a timestamp or a concatenation of the topic_id and comment_id ?
You would have to apply a the following tag in the comment portion of your template.
<a name"1215409"></a>
of course the number would be set to the comment id.
You're not going to be able to do this through htaccess. The fragment, the #something part of the URL, tells the client specifics on how to handle the content that it was served, in the case of anchors, it tells the browser where to seek to in the page. The fragment is never sent to the server, so apache never sees it, and thus nothing in the htaccess file can match against it or use it in any way.
Fragments are also used by javascript which can look at the URL to pull stuff out of the fragment or to force a script to rerun by reloading the page with a different fragment.
You can, however, send fragments to the client from the server, but there's no way to know whether the client already has the fragment or not. But the content itself will need to have the fragments in the links, htaccess isn't going to know which anchors are in the actual content that ends up being served.
Related
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.
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>...</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.
I believe the better way is using #comment-{comment_id} because the duplicate URLs problem that the other 2 URLs may cause.
I'm using http://simplemvcframework.com and I want to link to a section on a page normally you will just add something link
#section4
to the end of the URL but this doesn't work as expected, it doesn't jump to the correct section of the page. I'm suing the following format to try and achieve this:
/controller#section4
Do I need to pass the view into the link somehow? possibly something along the lines of
/controller/viewname.php#section4
I have never used that framework before, but anchors are handled by the browser, and don't even get sent to the browser.
(I.e. when accessing /controller/#section4, the server only receives /controller/.)
It looks like you don't know about the actual use of # in URLs:
Upon loading the page, the browser will look for an element on that page with an id or name (for backwards compatibility) matching the part after the #.
So you probably just want an element with id="section4" on that page.
If you need HTML 4 support, you have to put <a name="section4">...</a> around your anchor to achieve the same effect.
(See also this question.)
I'm building a site that that uses php and requires passing link id of each external link through the url and extracting the id using $_GET whenever a user clicks on any of the external links. Now, I need to rewrite the external url to momentarily point to my domain to enable me extract the link id before redirecting to the external destination, like what google does with search results before redirecting. Any help on how to do this will be highly appreciated.
I hope I understood what you were asking...
Have a database table of the conversions (or rather: the outside links) - the primary id can be the redirect "id".
Then have a file that you use for this outside-linking, eg "outsidelink.php".
Every link then needs to be written as outside.php?id=xxx and in the outsidelink.php you simply use the header() function to redirect the visitor (after you've recorded the fact of the request with all the details you want to; eg IP or userID, time, etc...)
Edit: oh, you first want to rewrite the external urls. I see. My bad.
Well, assuming the data where the external URL appears is some 'content' that comes from database, you need to scan the text before output for http://, compare the url (build a regex for this), if it looks like it's not matching the local host's url(s), then put it away in the aforementioned database, and rewrite the url to outsidelink.php?id=xxx
hope this time I got it right for you :)
Let's say I have a script, that redirects to another page based on your type of input.
Now that site you're redirected to already has a long query string. So what I'd like to do,
is append some html code to the end of the site without actualy sending GET or POST requests, let's say something like:
<?php
header("Location: redirectedsite.php");
//send extre html img for example
$html="<html><img scr='img.jpg'></img></html>";
?>
Is that even possible? I know about sessions and cookies, but I'd like to see if there are any alternatives.
Once the browser redirects to the other site, the body of your page gets ignored and ONLY the other site gets shown to the user.
So, unfortunately, what you want is not possible; if the redirected site is under your control you could conditionally add more contents based on a GET parameter, but it would still not work in the way you've described.
In fact, this would probably be a security nightmare if you could append HTML to any another website.
It's not possible in that manner no, sorry. If you control the destination site you could, as you say, set a session variable that would prompt the destination page to append the code, but there is no way to do it directly in the way you want. And neither option is possible if you don't control the destination.
Nope. When you do a header redirect, the browser will go there straight away. You can't add some HTML in between.
You would have to do that on the target page, or show a proper HTML page that then uses another kind of redirect (using the legacy <META> or JavaScript, or a combination of both along with a link that can be clicked manually).
Aside from appending it as a query parameter, or using session variables, you can't do this. Is there any particular reason you don't want to use sessions?
In part of a web page, I currently link to a part of the webpage using . In other words, I link to something like:
Link 1
However, on that same link I want to include a PHP variable, which would normally look like
Link 2
Combining the two in either order just makes the page refresh, neither scrolling properly or taking the php variable. How can I use the two simultaneously?
You can concatenate the two together, just put the hash last because that is not sent to the webserver (it will be used by the browser to jump to a certain part of the page):
Link 1
When you're on the same page as the URL in your anchor, regardless of #'s value (It's called the "fragment"), the page won't even refresh, it will scroll up in hope to find an anchor that has the fitting name for the fragment
The URL fragment (everything behind # at the end of an URL) is a client-side thing, PHP won't be able to do anything with it