Summary: User inputs a short-form/alias URL into a field (website.com
). I need to identify the full URL and replace the input with it (which could be www.website.com
or www2.website.com
based on redirection).
--
My web service allows users to input URLs. Upon submission, my service sends GET/POST requests to that URL from the server and processes the data received.
The issue arises when users enter short-form URLs like washingtonpost.com
instead of the complete https://www.washingtonpost.com
. While browsers handle this by automatically following the redirect, managing this logic on the server end is complex and I'd prefer to avoid it.
I am seeking a way to "validate" the entered URL. One approach is to add www
to all URLs without it, but not all websites follow that convention. Some use www2
, while others omit it entirely. The most reliable method seems to be listening for redirects and extracting the location
value from the response.
One idea involves using JavaScript to validate the URL by sending a GET request, analyzing the response for a redirect status code (301, 302, etc.), and replacing the input with the final destination URL.
The challenge here is CORS. This results in an error in the console:
XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.`
What would be the optimal approach to solve this? Preferably with JavaScript.