How to test for a HTTP URL redirect in PowerShell

Whenever you're browsing the web, you may notice when you click a link to one URL you end up being a totally different one. This is known as an HTTP redirect. HTTP redirects are one-way web server administrators can route traffic.

Whenever you're browsing the web, you may notice when you click a link to one URL you end up being a totally different one. This is known as an HTTP redirect. HTTP redirects are one-way web server administrators can route traffic.

A common use of redirects is when a link has already been published in numerous placed but the administrator wants to move the content. Rather than attempting to go to the fruitless effort of notifying everyone that used that URL he may instead simply put an HTTP redirect in to keep the link live but to forward the traffic somewhere else.

There are times when it's beneficial to detect these HTTP redirects. Perhaps you've got a script setup to download content from a website or you're a web server administrator and want to build a check to ensure the right URLs are getting redirected correctly. To do this requires making an HTTP call out to the web server in question and reading the response.

One way to do this is through PowerShell and the WebRequest class. In this article, we're going to cover how to build a PowerShell function that will query a specific URL and attempt to find out if that URL is being redirected to somewhere else.

To get started, let's build a small function for this task called Test-HttpUrlRedirect with a single parameter called URL.

We'll use the function shell above to place our code into so it can be reused if necessary at some other time.

Next, we need to, make the HTTP call. One way to do this in PowerShell is to use the WebRequest class under the System.Net namespace. The WebRequest class has a static method called Create in which we can pass a URL to invoke a HTTP call. We'll add the code to create the request into our function as shown below.

Using the WebRequest class under the System.Net namespace

Using the WebRequest class under the System.Net namespace

We now have a System.Net.HttpWebRequest variable ready to make the HTTP call to the server. Next, we need to actually make PowerShell go out and call the web server. To do that, we'll use the method. This will take our request we just built and send it to the web server recording the response that was received. Our function will now look like this:

Making PowerShell go out and call the web server

Making PowerShell go out and call the web server

We can now use the ResponseUri property of the HttpWebResponse to check for redirects.

URL provided to test doesn't match the URL that was returned which indicates an HTTP redirect was in effect for the URL provided.

Notice that the URL I've provided to test doesn't match the URL that was returned. This means that an HTTP redirect was in effect for the URL I've provided.  This is great but we need to incorporate this knowledge into our function. To do this, I'd like my function to return $true if a redirect was in effect or $false if it was not. I can do that by checking to see if the input URL matches the ResponseUri. If not, a redirect happened. If so, no redirect was in place.

Before we do this though, if you dive into the ResponseUri, you'll find the PowerShell is only representing ResponseUri as a simple URI string which, in fact, it's an actual object with other properties. We'll have to inspect a little closer to find out how to query that URI.

Inspecting to find out how to query that URI

Inspecting to find out how to query that URI

The OriginalString property looks like the best bet here to make the comparison.

Finally, we'll need to dispose of the response after we're done to clear it from memory which would finish it off to make it look like this:

We can now reuse this handy function for many different URLs.

Using the function for different URLs

Using the function for different URLs

 

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account