-1

On my website, users can share URLs.

These are stored as a text type in my database.

These links are displayed like so:

<a href="{{ $submission->link }}" rel="nofollow">{{$submission->title}}</a>

The problem with this is that blade adds the domain in front of URL if an href is detected.

So if the link is www.google.comit'll look like http://localhost/www.google.com

However if the link is https://www.google.com it'll work fine.

A solution I saw when I searched this problem is to put // at the start of the href, like so:

<a href="//{{ $submission->link }}" rel="nofollow">{{$submission->title}}</a>

This works fine for links that start with www., but if I do https://www.google.com the URL will look like this:

https//www.google.com

Notice the lack of :

What's the most elegant solution to this problem? Do I remove https before storing to the database? Do I make it so that if there's an https, I print the link as normal, but if there's no https I add //?

Felix
  • 2,532
  • 5
  • 37
  • 75
  • You are sure blade `{{ $var }}`does anything with domain / url string ? – Vincent Decaux Aug 08 '19 at 15:47
  • Use a ternary. Check to see if the string contains http. If it does, echo the link, if not then add the http/`//` – aynber Aug 08 '19 at 15:51
  • 1
    Blade isn't adding anything to your url, is href that tries to get a full url, see here: https://stackoverflow.com/questions/8764288/href-automatically-adds-to-current-page-url-in-php-cant-figure-it-out – dparoli Aug 08 '19 at 15:53
  • https://www.php.net/manual/en/function.parse-url.php – lufc Aug 08 '19 at 15:55

2 Answers2

2

www.google.com isn't a URL, it's part of one. URLs must include a scheme like http. Without one, they're treated as a relative link.

When your users submit URLs, use the url validation rule to validate the format. Alternatively, you can detect URLs without a http or https at the beginning, and either add it automatically or alert the user to fix it.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • I use 'link' => 'max:3000|regex:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/', in my validator. I would like it so that users don't need to add http or https if they don't have to. My question is, can I add https to their link even if the link only has http and not https? – Felix Aug 08 '19 at 15:59
  • @FelixMaxime Not really - not everyone has `https`. In *most* cases, `http` will redirect to `https` if the site supports it, and using `http` will match what the browser would do if you put `www.google.com` in there. – ceejayoz Aug 08 '19 at 16:01
  • Ok. In that case I'll save links to the database with http if there's no http/https provided. Thanks for the response. – Felix Aug 08 '19 at 16:02
1

Blade doesn't alter your data. It must be some sort of JS on your page or the link is malformed from the database. The solution is that you should add the protocol yourself if it is missing.

In your blade file, do something like:

<a href="{{ strpos($submission->link, 'http') === false ? 'http://' . $submission->link : $submission->link }}" rel="nofollow">{{$submission->title}}</a>

But I recommend that you should handle this in your model or the controller. Getters and setters should be used in your model to validate and construct the correct link to ensure that inconsistencies don't occur.

Rehmat
  • 4,681
  • 3
  • 22
  • 38