0

I have an HTML page that has several anchors on it:

<h4>Anchor 1</h4><a name="anchor1"></a>
<p>blah blah</p>
<h4>Anchor 2</h4><a name="anchor2"></a>
<p>blah blah</p>
<h4>Anchor 3</h4><a name="anchor3"></a>
<p>blah blah</p>

Then, I have a PHP array to relative paths:

$anchor_array = array(
    'site/path/#anchor1' => 'Anchor 1',
    'site/path/#anchor2' => 'Anchor 2',
    'site/path/#anchor3' => 'Anchor 3',
);

Each site/path/ has an index.php file inside so using .htaccess I'm able to use a link like this:

site/path/

This works fine, but when I add the anchor to it (eg,site/path/#anchor1) I get sent to the correct page, but not to the anchor location, just to the top of page.

What am I missing?

** EDIT **

After taking Salman A's advice, I was able to get 2/3 anchors to work properly. But one was still causing me trouble. I thought, perhaps, the browser is confusing the anchor name I have specified with something else. So I did a test and changed the anchor from anchor1 to the_anchor1 (actually, I changed it from history to our-history) and it works now.

Is there a reason using history as anchor name is bad?

nicorellius
  • 3,715
  • 4
  • 48
  • 79
  • Do any of those anchor targets require the page to scroll? If the target page is short enough and/or none of the targets require scrolling, the browser can't move "down" to an anchor, because there isn't enough page height available to do that kind of move. – Marc B Nov 09 '12 at 20:54
  • Good question... The page is long enough that it should anchor to the correct location. Scrolling is required, yes... – nicorellius Nov 09 '12 at 20:57
  • (i) Does the `#anchor1` appear in browser address bar? (ii) does the landing page contain ``? – Salman A Nov 09 '12 at 21:10
  • Yes. Here are the answers: (i) result looks like this: `http://localhost/site/path/#anchor1/` and (ii) viewing source results in this: `` – nicorellius Nov 09 '12 at 21:24

1 Answers1

1

Regarding:

  1. result looks like this: http://localhost/site/path/#anchor1/
  2. viewing source results in this:

You have a trailing slash in the hash portion of your URL. The browser will look for a named anchor with name=anchor1/ (or id=anchor1/) which is not there obviously. Locate where the trailing slash is coming from and remove it.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • I had originally tried your suggestion and it was giving spurious results. For example, after changing it back, the anchors seem to work partly. I moved them around a bit to see if I could tease them into working and one still isn't bring me to the correct spot. You are basically correct, but I wish I coujld figure out the odd behavior. – nicorellius Nov 11 '12 at 18:21
  • Your solution was correct - thanks. I had another issue confounding the results. See edit above. – nicorellius Nov 11 '12 at 18:28