Skip to content Skip to sidebar Skip to footer

Why Do My Hyperlinks Go To The Wrong Directory?

In my project functionality is like when i need to set a link to direct to: /site/sales/sold.php so the menu.php file link would look like this:
  • Solution 1:

    If you are on a page sales/ and link to a page sales, the link will result in sales/sales/ – that is the defined behavior.

    The default method to avoid that is to specify a base URL via the HTML <base> tag. There is no PHP, JS or CSS involved here – just good ol' HTML 2.0.

    <base href="http://www.example.com/site/">
    

    Using this in your pages head, will define the given URL as base for all links on the page, thus a link like sales/ will actually result in http://www.example.com/site/sales/ when clicked.

    See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base


    Solution 2:

    Why don't you just use absolute Links? Or even URL-rewriting?

    The only way of using relative links like your "sold.php" is if your menu only occurs inside your /sales directory. If this menu comes up for example inside a page on /site, of course it won't work, because there probably isn't a file called "sold.php" under /site/sold.php. So if you wan't to make sure, your link "Sold" always points to the right direction, use a Link like /site/sales/sold.php. Then it won't matter if you are inside /site or even inside /site/sales.

    if i for example set my menu link to direct to: /site/sales/sold.php so the menu.php file link would look like this:
    
    <li><a href="sales/">Sales</a></li>
    <ul>
    <li><a href="sold.php">Sold Items</a></li>
    </ul>
    

    This does not make sense. If you would point your link to /site/sales/sold.php, your link would look exactly like that: <li><a href="/site/sales/sold.php">Sold Items</a></li>


Post a Comment for "Why Do My Hyperlinks Go To The Wrong Directory?"