Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

If I have a broken link it gets logged with a 404. If I have a link to an anchor and the anchor is missing, I don't get an entry in the log. How can I find those broken anchor links, especially if the traffic is coming from a referrer? Is there a way to configure the logging to capture this information?

HTTP Server: Apache 2

share|improve this question
    
+1, I've often wondered if there was a practical (automagic) way to do that. –  Tim Post Jul 12 '10 at 2:39

4 Answers 4

up vote 11 down vote accepted

It is impossible to configure logging of the #fragment portion of the URL because your web server never sees it. From RFC3986

the fragment identifier is separated from the rest of the URI prior to a dereference, and thus the identifying information within the fragment itself is dereferenced solely by the user agent, regardless of the URI scheme. Although this separate handling is often perceived to be a loss of information, particularly for accurate redirection of references as resources move over time, it also serves to prevent information providers from denying reference authors the right to refer to information within a resource selectively. Indirect referencing also provides additional flexibility and extensibility to systems that use URIs, as new media types are easier to define and deploy than new schemes of identification.

What this means is that the web browser basically strips off the anchor part of the URI before sending it to the web server. The HTTP protocol doesn't even deal with fragments because of this; they're never sent over the wire.

Fragments are available to JavaScript (because it's client-side) as part of the location.href property. Once obtaining the fragment name, you can confirm that it is valid by searching the document.anchors[] array elements' name property for that value.

Once you've done this, it can be logged using AJAX or, as pelms notes, passed to Google Analytics or similar services.

share|improve this answer
    
If the anchor is just <a name="blah"></a> you won't be able to determine existence with getElementByID; it might work in IE (I'm not sure whether it's still the case in 8), but if you want to do this, I'd suggest adding the id as well. –  Cebjyre Jul 12 '10 at 13:08
    
Hmm. Gapped a bit there :) Yes, adding the id is most efficient, or you could walk the DOM by name or tagname. I'll fix that. –  JasonBirch Jul 12 '10 at 16:48
    
I wonder if html5 anchor ping could be used for this purpose. –  Kzqai Apr 22 '11 at 19:05

I don't think its possible to see this in Apache's logs. If a file is requested and served, that pretty much ends Apache's involvement (or any other server). Finding and positioning to the anchor is something the browser does. By the time your browser gets to locating the anchor, the connection to the server is already closed.

A good link checker should pick that up, but that's a manual process.

share|improve this answer

CSE HTML Validator checks for missing anchors... for a price.

See http://www.htmlvalidator.com/htmlval/whycseisbetter.html

share|improve this answer

As others have said, page anchors are something that the browser operates on rather than the web server.

You could use some code to pass the anchor info on to your Analytics program. For example you could extract the location.hash value and pass it to Google Analytics as a custom variable.

More usefully you could run code that checks the page html for an anchor that corresponds to the value of location.hash and if it's missing triggers a GA event. That would then give you a GA report on broken page anchor links as users click on them.

share|improve this answer
    
Great idea. There is so much I could be doing with GA but am not, probably leaving money on the table... –  JasonBirch Jul 12 '10 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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