0

Sometimes, links open pages in my website with weird text at the end of it (like Facebook). I would like to remove that text, since my page will not display with it there.

For example, what I would like to do is tell my page that if the URL has text after a pound sign, remove it and open the URL without it.

So if someone opens my page with: http://www.example.com/news/stories/this-is-a-news-story/#sthash.MmwTdqVa.dpuf I want it to correct and open the page http://www.example.com/news/stories/this-is-a-news-story/

How would I do that? I know how to do this with PHP, but I'm new to ColdFusion.

Thanks! Brendan

Brendan
  • 107
  • 2
  • 13
  • 4
    _my page will not display with it there_ - Having any additional information in the URL should not affect your page from being displayed. If you are not using a URL variable then it is simply ignored. Why do you want to handle this with ColdFusion? While ColdFusion can do what you are asking it is not designed for that. A web server is. If you truly want to modify the URL then you should use your web server to do so. But you will be putting yourself in a maintenance nightmare. Users can type whatever they want in the browser address bar. Are you going to keep making rewrite rules? – Miguel-F Jan 19 '16 at 19:52
  • In general, if you can do something with php, convert the code to a description of what is does. Then look for ways to accomplish it with ColdFusion. I this case, listen to Miguel. – Dan Bracuk Jan 19 '16 at 20:35
  • 2
    The stuff after the `#` (called fragment) is not part of the HTTP request, thus not submitted to the server. You can only handle the fragment on clientside by means of Javascript: `window.location.hash` – Alex Jan 19 '16 at 23:59

1 Answers1

0

Are you using a service like AddThis.com? If so, these (and other) marketing services may add fragment to URLs for tracking purposes. They're harmless, but here's AddThis instructions on how to remove them if you use their service. (Check w/your marketing team before you do this.)

http://www.addthis.com/academy/removing-hashtags-anchors-and-tracking-codes-from-your-urls/

The fragment is not passed to the ColdFusion server and not in the CGI scope. It's intended to be available and used in the client browser only. This should not be causing any problems with "ColdFusion" generating pages, so your problem may due to javascript. Open up Web Developer tools (F12) to identify any javascript errors that may be caused by the unexpected fragment. (You didn't provide a URL or error message, so it's difficult to troubleshoot the problem you may be encountering.)

Here's an existing solution on StackOverflow that you could you use to remove the fragment client-side:

https://stackoverflow.com/a/13824103/693068

// remove fragment as much as it can go without adding an entry in browser history:
window.location.replace("#");

// slice off the remaining '#' in HTML5:    
if (typeof window.history.replaceState == 'function') {
  history.replaceState({}, '', window.location.href.slice(0, -1));
}

I would only advise performing this once during pageload and maybe include a filter to preserve any fragments you actually want to preserve.

As a side note, I occasionally add <a href="#top">Go to Top</a> to long pages without any matching #top element. Normally any id that is not found will force the browser to automatically scroll to the top.

Community
  • 1
  • 1
James Moberg
  • 4,360
  • 1
  • 22
  • 21