4 Leave Cover 4 Leave Cover - 1 month ago 7
HTML Question

Why does an HREF consisting of a plain forward slash direct to the homepage?

I came across an HTML anchor which reads

<a href="\">Home</a>
.

Normally we put something like
<a href="index.php">Home</a>
but when I click on
<a href="\">Home</a>
I am able to go to the index page on the website.

I can't replicate the behavior on localhost.

Why does
\
direct to the website's homepage, and was it intentional on the developer's part?

Answer

You are correct that it is incorrect, and it's almost certainly not intentional. Backslashes (\) are considered unsafe in URLs, and if a backslash is necessary in your URL you would normally have to encode it as %5C.

Why it works

As Rocket Hazmat pointed out in a comment on your question, most browsers automatically substitute / for \ in URLs.

So the link to \ is converted to /, which requests the root of the current server. The server is probably set up to serve some default file like index.php when it receives a request for a directory, and the result is loading the homepage.

Why it doesn't work in localhost

I don't know your local http server setup, but chances are it hasn't been configured to serve a specific page (like index.php) when it receives a request for a directory. So you are likely just seeing a directory listing of whatever is at the root of the local http server you are running locally.

Comments