How to check if a redirect leads to a different domain

2.5k views Asked by At

I have a huge number of urls or domains and I want to determine whether they perform a redirect to a different domain.

When I check for status code 302, I can figure out which ones do a redirect. However, some of them just perform a redirect from somedomain.com to somedomain.com/somename.php, whereas the ones I need are those that do a redirect from somedomain.com to otherdomain.com.

I'm working with Python and the requests library and I have a somewhat limited knowledge when it comes to programming languages. If you have an idea that specifically requires me to use a different language, I suppose I can figure it out.

1

There are 1 answers

2
Tuan Anh Hoang-Vu On

If you are using python-requests, you can use request.history to get all redirections, except the final hop, which in turn will be available in request.url. For example:

r = requests.get("http://go.microsoft.com/fwlink/?linkid=99104")
for h in r.history:
    print h.url
print r.url

http://go.microsoft.com/fwlink/?linkid=99104
http://office.microsoft.com/en-us/word/HA100319991033.aspx
https://support.office.com/en-us/article/HA100319991?CorrelationId=f32d077d-1c5b-4d12-ba24-8c500f36c5d1

If there is no redirection, then request.history will be an empty list.