Skip to content Skip to sidebar Skip to footer

How To Deal With Www/http In Href

I have a db with a buch of urls. The values were entered by users, so it might be something like www.domain.com or http://www.domain.com or stackoverflow.com or https://something.c

Solution 1:

The www. bit is not special at all, people rely on an automatic correction feature of most browsers to prepend it if the host does not exist. To replicate this, you need to run a program that attempts to resolve each of the host names in your database, and retries with an extra www. if that fails.

The http:// bit is easy: if it is missing, add it.

Solution 2:

There are two ways to handle this situation:

First, validate the user input. At the time a URL is submitted, validate it (preferably on the client side via Javascript) to ensure it has the required elements.

Second, in your code, you can use a regular expression or even simple pattern matching to ensure that the string starts with 'http://' or 'https://', and prepend it as needed.

The implementation details vary from language to language, but the concept is the same.

Post a Comment for "How To Deal With Www/http In Href"