0

I am trying to make users input links into their posts, onto a post page called mysite.com/subfolder/ using a form input.
In the result, if someone enters google.com, it displays
<a href="google.com">google.com</a>
when clicking on the link, it doesn't go to google.com, instead it links to mysite.com/subfolder/google.com.
Is there a way to prevent this from happening?

*Edit: I fixed this by entering making the input url, but other solutions would be great!

Dbw
  • 1
  • 2

1 Answers1

0

You have to include the scheme (eg https) in the link -- otherwise it'll be treaded as relative to the current page. In other words, you'll want the whole https://google.com

You don't include any info about how you're getting this information from the user, but it would be a good practice to check it using something like RegEx for a scheme and append one if it doesn't exist.

You can also use Javascript to prepend a scheme to URLs where it doesn't exist, but you'd have to make sure you don't affect any other links that were meant to be relative. Here's an explanation of how to do that: Prepending "http://" to a URL that doesn't already contain "http://"

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Thank you! I was using a form with a input tag to enter the URLs, but I'll look into what you mentioned. – Dbw Jan 11 '21 at 23:47
  • If you include some code about how your accept and store that info, you might get a more specific response. Otherwise, feel free to upvote and or accept my answer – jnpdx Jan 11 '21 at 23:49