I have a file index.php
containing mostly HTML and a bit of PHP. I have declared id
s for some elements (e.g., <h2 id="contact">Contact</h2>
) and provide links to them (cf. below).
<ul>
<li><a href="#contact">Contact</a></li>
</ul>
The links work fine when clicked (i.e., the user is taken to the anchor point), but they point to, e.g., index.html#contact
, so that when the page is reloaded, you get a 500 error.
How can this behaviour be avoided? And why does it occur anyhow?
I'm using the YAML CSS framework btw.
UPDATE: Why don't you hack it with js :)
location.replace("http://www.w3schools.com");
UPDATE END
The hash (#) inside link means an "ID" of an element within the same page.
href="#id" .. means index.php/#id <-- scroll to an id element
href="link" .. means index.php/link <-- redirect to file called 'link'
Also you need in htaccess enable to redirect to file without file extension. e.g. php / html
Your problem
Check your existing htaccess
Try to use this :
<ul>
<li><a href="#contact">Contact</a></li>
</ul>
<h2><a id="contact">Contact</a></h2>
or
<script>
function getPosition(element){
var e = document.getElementById(element);
var left = 0;
var top = 0;
do{
left += e.offsetLeft;
top += e.offsetTop;
}while(e = e.offsetParent);
return [left, top];
}
function jumpTo(id){
window.scrollTo(getPosition(id));
}
</script>
<ul>
<li><a href="#" onclick="jumpTo('contact');">Contact</a></li>
</ul>
<h2><a href="#" id="contact">Contact</a></h2>
</div>
You can use javascript to scroll on your element so URL doesn't change and when you reload the page has the starter URL.
That's weird because it is supposed to work. As others suggest, have a look at your error logs. In the mean time you can try this quick fix:
Instead of having #contact
in your <a>
you can put the full url using the help of php like this:
<html>
<head>...</head>
<body>
<ul>
<li><a href="<?php echo getPageUrl(); ?>#contact">Contact</a></li>
</ul>
<h2 id="contact">Contact</h2>
</body>
</html>
<?php
function getPageUrl(){
return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
?>
This should make your <a>
look like this
<a href="http://example.com/index.php#contact">Contact</a>