简体   繁体   中英

IIS Url Rewrite Module: Get ApplicationPath

I am looking for a way to rewrite the url in case the application path in the url has a different casing. Since the application path can vary for different deployments, I need to access it dynamically. Is there any way of doing it?

Background:

I am setting path of cookies to the application path. Since cookie path is case sensitive, I need to rewrite urls in case they are wrongly cased. I would also like to have alternate ways that do not need the use of the url rewrite module.

Example

Let's assume that for one deployment, the alias for the application is "ApplicationA" (for another deployment, the alias may be "ApplicationB").

http://<host>:<port>/<applicationA or Applicationa or APPLicationA etc.>/<rest of the url>

Redirect to 

http://<host>:<port>/ApplicationA/<rest of the url>

Not sure if REWRITE is correct operation in your case, maybe you should use REDIRECT(permanent), but below is rule which allowed me fetch Application name in specific case:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="My Rule" stopProcessing="true">
                <match url="^(.+)" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="TmP/.+" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_URI}" pattern="tmp/(.+)" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="TmP/{C:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

I think creating and adding a custom Http Module can solve your problem. An HTTP module is called on every request in response to the BeginRequest and EndRequest events.

You can access the URL dynamically and redirect it by changing its case.

private void PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        var f = req.Url;
        f="Change case of URL";
        if (condition)
        {

            app.Response.Redirect(f);
        }

    }

Updated

May I suggest you subscribe to the BeginRequest event using a HTTPModule.

With the RewritePath method you would gain speed against Redirect , where you just match and rewrite the url if the casing is wrong, ... or actually just adjust the casing might be faster than to check it first and then adjust (test and see before you pick solution).

A positive side effect is you can easily put in other tests and make exceptions etc.

public class AdjustCasingModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += OnBeginRequest;
    }

    protected void BeginRequest(object sender, EventArgs e)
    {
        var httpContext = ((HttpApplication)sender).Context;

        string[] path = httpContext.Request.Path.Split('/');

        if (path[1].Length > 0)
        {
            Regex rgx = new Regex(@"^[A-Z][a-z]*[A-Z]");

            if (!rgx.IsMatch(path[1]))
            {
                char[] a = path[1].ToLower().ToCharArray();
                a[0] = char.ToUpper(a[0]);
                a[char.Length - 1] = char.ToUpper(a[char.Length - 1]);
                path[1] = new string(a);
                httpContext.RewritePath(String.Join('/', path));
            }
        }
    }

    public void Dispose()
    {

    }
}

Side note:

I still recommend using lower case path in the first place.

Just a thought, if one would consider abandoning the camel case notation (ApplicationA) in favor of forced for example lowercase* (applicationa), you could use the ToLower keyword as below.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="force lowercase" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
                </conditions>
                <action type="Redirect" url="{ToLower:{URL}}" redirectType="Temporary" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

*If you are committed to your original camelCase notation in the url then I would defer to Uriil's approach above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM