Redirect with URLRewrite based on QueryString

Consider a situation where you need to redirect your Application to the Error page when you receive a particular value in your query string. This can be done using URL Rewrite with an inbound rule either directly in IIS or add rules directly in your web.config.

Below configuration can be added in web.config:

<system.webServer>
	<rewrite>
		<rules>
			<rule name="BlockQS" enabled="true" stopProcessing="true">
				<match url=".*" />
				<conditions>
							<add input="{QUERY_STRING}" pattern="P=abc" />
				</conditions>
				<action type="Redirect" url="https://website.com/ErrorMessage.aspx" appendQueryString="false" />
			</rule>
		</rules>
	</rewrite>
</system.webServer>

If you check directly under IIS URLRewrite feature for your website, it would look like this:

URL Rewrite

Now, when you try to access https://website.com/?P=abc, it would redirect to the configured error page.

To install URL Rewrite, follow the link.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.