Anchor HREF, if I click again the anchor the link

2019-08-26 01:26发布

问题:

Good Day! I've got some trouble on PHP. If I click an href which is this <a href = 'members/loans/loan-application-form.php'> it goes to the loan-applicaton-form.php, but if i click it again inside the loan-application-form.php (cuz it is located at the navbar that exist in all pages) the href of the anchor concatenates on the existing url and it creates an error telling that address is not existing. I already knew that the problem is it searches the address on the current directory and probably it will return an error because there is no existing address on the directory because it is the origin or the parent. What is the best way to avoid this?

回答1:

Add a base tag to your header. This will prepend all HTML URL's with the given base URL. (Including images, CSS and JS calls)

<head>
    <base href="http://www.yourdefaulturl.com/">
</head>

Your URL will then be http://www.yourdefaulturl.com/members/loans/loan-application-form.php



回答2:

You can use an absolute path

<a  href = 'http//your.site.cpm/members/loans/loan-application-form.php'>

Advice : you can read this article

or this stack question

Or you can use ../ to navigate inside your relative path

Lets imagine your nav bar is located at /navigation/navbar.html

Then you can have a relative path like

<a  href = '../members/loans/loan-application-form.php'>


回答3:

By specifying relative Urls like so:

<a  href = 'members/loans/loan-application-form.php'>

You are simply stating you wish to go form the current page to this page, hence why it is being added to the end of the current URL. The best way to avoid this quite simply is to set an absolute URL like so:

<a  href = 'http://projectname/members/loans/loan-application-form.php'>

Even when not using http:// it is often still considered relative so be sure to include this.

Another way to do the same but slightly quicker would be to add a variable in say your header file for example:

$url = 'http://example.com';

Then when specifying the URLs you can do say:

<a  href = '<?php echo $url;?>/members/loans/loan-application-form.php'>

This just means that should you say change your domain, rather than editing every URL you can simply edit the variable value and be done with it.