hello everyone .. i am pretty new to php programming and web development and i need to ask this. i have designed a few websites for my clients and i am getting better and now i have started learning php to add dynamic feel to the websites. my question is whenever i create links in my pages i do something like <a href="index.php">HOME</a>, <a href="aboutUs.php">ABOUT US</a> and so on. So i uploaded the website and whenever i view the source code the links appear exactly like that yet in other professional websites i have seen i always see something like <a href="">HOME</a> for example.
what am i doing wrong. how can i make better links to other pages. what is the use of building links in that format. thank you.

Dani AI

Generated

Short answer: nothing is wrong — using just a filename in an href is a normal relative link and works for internal navigation. Professional pages that show a full http://... URL in the HTML are usually linking to an external site, exposing the absolute address for a feed/email, or intentionally emitting a canonical/absolute URL for systems outside the site. As , and already hinted, choose the form that fits your use case.

Practical guidance:

  • Use filename-only (relative) links when the target is alongside the current page and you expect directory-relative resolution. This is fragile when pages live in different folders.
  • Use root-relative links (begin with /) for site navigation so paths resolve from the webroot regardless of the current directory.
  • Use absolute (scheme + host) URLs when links will be consumed outside the site (emails, RSS, redirects, canonical tags) or when linking to another domain.

A simple, maintainable approach is to centralize URL generation in one place (config or helper) and build links from a single base value. For example, set a BASE_URL in your configuration and prepend it when you need full URLs; keep template links root-relative for normal navigation.

Other practical notes and cautions:

  • Avoid protocol-relative //example.com links today; prefer explicit https://.
  • Do not add rel="nofollow" to your internal navigation — reserve that for untrusted or sponsored outbound links.
  • Be careful with a <base> tag: it changes how all relative URLs resolve.
  • If you want cleaner visible URLs (no .php), use server rewrites or a routing layer rather than hardcoding file names.

Recommended Answers

All 3 Replies

the easiest way to do this is

<a href="<?php echo "http://".$_SERVER['HTTP_HOST']; ?>/yourpage.php">Click Me!</a>

but I'm not realy sure thats what you wanted. Do you mean how do you get your home page to be called home.php?

Member Avatar for Member #120589

I think I understand what you mean. The full url (...) is only really necessary if you're linking to external websites. Your own urls are fine. The only thing you have to look out for is 'relative' and 'absolute'. For me, making urls absolute (begin with '/') saves a lot of hassle.

For pages in your site, you don't need the http part. I think Ardav has provided a better explanation on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.