1

Is it possible to create a macro to check the redirect links from cell A1:A10 ?

For example: on cell A1 I have the link "www.pplware.com" but when we open the link www.pplware.com we are redirect to another site "https://ppware.sapo.pt"

Is it possible to write the redirect link to cells B1:B10 ? B1: https://ppware.com.sapo.pt

1 Answer 1

1

As long the redirect is done using HTTP (301 moved permanently) we can use WinHttpRequest object to test. Note: XMLHTTP is not possible since this will not allow disabling follow redirects.

WinHttpRequest object has an property Option which is a enumeration WinHttpRequestOption enumeration where the 6. item is WinHttpRequestOption_EnableRedirects. This is default setted True and so WinHttpRequest will follow the redirect immediately. But we can set it False and so get the response header before the redirect is followed. From that we can get the Location: which is the URL where the redirect goes to.

Example:

enter image description here

Public Function testRedirect(oCell As Range) As String testRedirect = "not redirected" strURL = oCell.Hyperlinks(1).Address WinHttpRequestOption_EnableRedirects = 6 Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1") oWinHttp.Option(WinHttpRequestOption_EnableRedirects) = False oWinHttp.Open "HEAD", strURL, False oWinHttp.send "" If oWinHttp.Status = 301 Then strResponseHeaders = oWinHttp.getAllResponseHeaders() For Each strResponseHeader In Split(strResponseHeaders, Chr(10)) If Left(strResponseHeader, 9) = "Location:" Then testRedirect = "redirected to " & strResponseHeader End If Next End If End Function 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Axel Richter... It works fine. You are the boss ;)
Why not use .getResponseHeader("Location") ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.