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>开发者_如何学C;
<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.
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, e.g. . More info here
If you would like all URLs to be relative to that base URL, you can use HTML's <base>
tag in your <head>
like so:
<base href="http://localhost/balibar.co/">
<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']; ?>">
精彩评论