简体   繁体   中英

anchor <a> + Link to base URL

If I'm on a page like

http://localhost/balibar.co/?dating=dating-articles-and-information

and I want to have anchor that links to the base URL being

http://localhost/balibar.co

Is there a way to do this without hard coding the URL?

I've tried:

 <a href="/"></a>
 <a href="#"></a>

will have a few domains use this page so I don't want to hard code the domain name if possible.

<head>
    <base href="http://www.google.com" />
</head>
<body>
    <a href="">Google?</a>
</body>

That link will now go to google.com

Here is the proof:

http://jsfiddle.net/3wXCJ/

You use HTML's <base> tag to specify the base url for all elements that use the href attribute. Now, any tag with an href or src attribute that is empty, it will automatically go to the url you specified in the base tag by default.

Assuming the page can load fine as http://localhost/balibar.co/ then a relative path with a single dot (.) will take you to it <a href="./"></a> I believe you can also use a single dot by itself without the slash <a href="."></a>

The single dot (.) in the relative path represents the current directory.

如果您希望所有URL都相对于该基本URL,您可以在<head>使用HTML的<base>标记 ,如下所示:

<base href="http://localhost/balibar.co/">

The HTML <base> tag may suit your needs.

<html>
<head>
<base href="http://www.yahoo.com/images/" />
</head>

<body>
<!-- Links to http://www.yahoo.com/images/ -->
<a href=".">Top-level link</a>
</body>
</html>

Note that the href attribute will also affect your image and artifact urls, eg . More info here

<a href="/"></a> only works if the host sees balibar.co as an index page.

Otherwise you'll have to go <a href="/balibar.co"></a>

from what I understand about SEO, you are better off coding the domain name, rather than an anchor.

because you have several domain names, I would use PHP to first figure out what domain name is currently used, and then write it to the link:

<a href="<?php echo 'http://'.$_SERVER['SERVER_NAME']; ?>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM