1

I have external links on my webpage:

<a target="_blank" rel="nofollow" href=<?php echo $r['url'] ;?>>
                                                VISIT STORE
                                               </a>

When $r['url'] has http:// it shows the correct external url but when it has only www or just the website name with the domain it appends the web page's url.

    Case 1 : url = http://google.com    Works fine
    Case 2:  url = www.google.com      creates a link as: http://localhost/appname/controller/action/www.google.com
Varun Jain
  • 1,901
  • 7
  • 33
  • 66
  • I am also facing the same problem. If you found the solution for this, please share with me. – Nuju Jun 20 '17 at 09:46

3 Answers3

1

Yii has no any relation to your problem. You don't know difference between absolute and relative URL's.

In your code, you don't use Yii anywhere. Yii has very powerfull URL manager with methods: createUrl, createAbsoluteUrl and other. You don't use this.

You need to understand the difference between absolute and relative URL's and your question gone away. There are more information in internet and in StackOverflow too: Absolute vs relative URLs

Community
  • 1
  • 1
BaBL86
  • 2,602
  • 1
  • 14
  • 13
0

try this:

<?php echo CHtml::link('Google', '//www.google.com', array('target'=>'_blank')) ?>
Goodnickoff
  • 2,267
  • 1
  • 15
  • 14
0

Hope all your URLs are external. Then check your url for http:. If url contains http, then use directly, Otherwise add.

function is_valid_url($url)
{
   if(strpos($url, "http://")!==false)
      return $url; // correct
   else return "http://$url";// http not found.
}

In your a tag

<a target="_blank" rel="nofollow" href=<?php echo is_valid_url($r['url']) ;?>>
   VISIT STORE  </a>
Kumar V
  • 8,810
  • 9
  • 39
  • 58