John R
John R

Reputation: 123

IIS URL Rewrite to querystring parameter

I need to capture requests to folders that aren't file folders to be redirected to a default page with the requested path as the querystring.

For example, if the user requests

www.mysite.com/IT

I want it to redirect to

www.mysite.com/default.aspx?q=IT

However, if they request a page that is in a real sub-folder, that shouldn't be redirected. For example, requests to www.mysite.com/projects/new.aspx shouldn't try to do any re-directions as it's a physical folder and file in the site.

Upvotes: 1

Views: 2137

Answers (2)

hardkoded
hardkoded

Reputation: 21715

Use the IIS Rewrite module and the set it up in the web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirector" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions>
                        <add input="{PATH_INFO}" pattern="/*" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/default.aspx?q={PATH_INFO}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Upvotes: 1

Michael11B12
Michael11B12

Reputation: 296

You could define a 404 error page that would redirect to your default page with the "q=" that you desire

Upvotes: 0

Related Questions