39

Longtime reader of stackoverflow but first question.

I'm working with Wordpress (specifically thesis theme) in the custom_functions.php file and am finding for some reason is automatically adding the current page url. For example this code is to query the database and then loop through outputting each product in it's own div:

$result = db_connection($query);
while ($row = mysql_fetch_array($result)) { ?>
    <div class="box"><a href="">
        <img src="http://www.electricbikehub.co.nz<?php echo $row['product_root_directory']         . $row['mid_size_image'] ?>">
        <h2><?php echo $row['name']?></h2>
        <p><?php echo $row['description_brief'];?></p>
        <p><span class="multiple_product_red"><span class="multiple_product_linethrough">RRP: <?php echo $row['list_price']; ?>.</span> Our discounted price: <?php echo $row['our_price']; ?>. Includes delivery and GST.</span></p>
        </a>
    </div>
<?php } ?>

As you can see 3rd line says href="" but the actual link being generated is the current page (in this case 'http://www.electricbikehub.co.nz/?page_id=1192'). If I add anything in the href, such as href="something" it will just add it to the end, ie http://www.electricbikehub.co.nz/?page_id=1192something.

Any help would be greatly appreciated!

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Evan Hobbs
  • 3,552
  • 5
  • 30
  • 42
  • 1
    Does it do the same thing if you put a full url in the href starting with http like http://something.com? – davidethell Jan 06 '12 at 20:51
  • That doesn't sounds right. Pics or it didn't happen. – Ignacio Vazquez-Abrams Jan 06 '12 at 20:51
  • I can't quite figure out your question... what _do_ you want in the href attribute? – Grexis Jan 06 '12 at 20:51
  • 2
    I think I figured it out thanks to wescrow. As I said below if I did href="www.google.com" it would just append it to the end of the root domain name (ie www.electricbikehub.co.nz/www.google.com). But if I added the 'http://' it would be fine. That was my problem... – Evan Hobbs Jan 06 '12 at 21:31

9 Answers9

53

This is how a browser interprets and empty href. It assumes you want to link back to the page that you are on. This is the same as if you dont assign an action to a <form> element.

If you add any word in the href it will append it to the current page unless you:

  • Add a slash / to the front of it telling it to append it to your base url e.g. http://www.whatever.com/something
  • add a # sign in which case it is an in-page anchor
  • or a valid URL

EDIT: It was suggested that I add a link to help clarify the situation. I found the following site that I think does a really good job explaining the href attribute of anchor tags and how it interprets URL paths. It is not incredibly technical and very human-readable. It uses lots of examples to illustrate the differences between the path types: http://www.mediacollege.com/internet/html/hyperlinks.html

Wes Crow
  • 2,971
  • 20
  • 24
25

Add http:// in front of url

Incorrect

<a href="www.example.com">www.example.com</span></p>

Correct

<a href="http://www.example.com">www.example.com</span></p>
Mukesh
  • 7,630
  • 21
  • 105
  • 159
  • Why does this solution work even when the correct URL is `https://www.example.com`, not `http://www.example.com`? – Arya Oct 05 '18 at 08:00
  • 1
    A more agnostic value that won't force a specific schema is `//www.example.com`. Nowadays everything should be with TLS, but you never know, it can be useful for flexibility in a development environment without having to setup a certificate. – Kamafeather Nov 16 '21 at 17:22
10

if you want to redirect it to some other url lets google.com then make your like as happy to help other says rikin <a href="//google.com">happy to help other says rikin</a> this will remove self site url form the href.

Rikin Adhyapak
  • 483
  • 5
  • 8
3

You can just put // in front of $yourUrl in href:

<a href="//<?=$yourUrl?>"></a>
strix25
  • 543
  • 2
  • 10
  • 22
2

A solution that works no matter if you are developing on a local server or live is to add "//" in front of your link. This will effectivly remove the URL of the site you are currently on.

Example:

<a href="www.google.com">Google.com</a>

This will in output http://localhost/mySite/currentPage/www.google.com

What you should do instead is this:

<a href="//www.google.com">Google.com</a>

This will output www.google.com

1

You do realize this is the default behavior, right? if you add /something the results would be different.

you can do a number of things to prevent default behavior.

href="#":

Will do nothing but anchor - not the best solution since it may jump to page top.

<a href="#">

href="javascript:void(0);"

Will do nothing at all and is perfectly legit.

<a href="javascript:void(0);"></a>

href="your-actual-intended-link" (Best)

obviously the best.

<a href="<your-actual-intended-link>"></a>

If you don't want an a tag to go somewhere, why use an a tag at all?

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
0

eI also have the same issue with that, it's just like other answers from before but then when I tried using that it still adding more and more to the URL. So I add something into my code.

from this :

<a href="//<?= $_SERVER['HTTP_HOST'] ?>something/about">about</a>

to :

<a href="///<?= $_SERVER['HTTP_HOST'] ?>something/about">about</a>

if you notice I add one backslash "/" on the href="///" and not only using two backslashes

2bytebrain
  • 21
  • 2
  • 2
-2

In any case, your code will generate invalid markup: You shouldn't wrap block contents in a link. tags don't work like this. If you want this effect you should use js or create an absolutely positioned link above the content (z-index). More on this here: Make a div into a link.

You should make sure to validate your code when it renders: http://validator.w3.org

Community
  • 1
  • 1
Ben D
  • 14,321
  • 3
  • 45
  • 59
-4

Use this:

<a href="<?php echo(($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">Whatever</a>

It will create a HREF using the current URL...

Frederick Marcoux
  • 2,195
  • 1
  • 26
  • 57
  • 1
    I'd be very careful using this, unless you force path rewrites with .htaccess or PHP (code injection). Also, you should include `($_SERVER['HTTPS'] ? 'https://' : 'http://').` at the beginning since `$_SERVER['SERVER_NAME']` only returns `www.somedomain.com` (see [$_SERVER documentation](http://php.net/manual/en/reserved.variables.server.php)) – 0b10011 Jan 06 '12 at 21:47
  • Indeed, but if he haves a HTTP website, he just haves to include the HTTP:// or HTTPS:// before the PHP code... The protocol is not changing frequently! – Frederick Marcoux Jan 07 '12 at 04:03
  • No, but when it does, if you include that code, he won't have to find each and change it manually (and redirects for every link can increase page load times). Either way though, it's a good idea to at least mention in your answer that the http(s?):// is required as well as note that escaping of `$_SERVER['REQUEST_URI']` is necessary. – 0b10011 Jan 07 '12 at 18:53