0

I want to write a rule for a redirect in my web.config.

I want to redirect urls like...

http://www.example.net/profile/username

to...

http://www.example.net/#/profile/username

I have added the below run but it doesn't seem to work. What is the best way to do this?

  <rule name="Redirect to hashed profile" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="^.net/profile/*"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/#/{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
        </rule>

1 Answer 1

2

Here's what's going on and how you fix it.

pattern="^.net/profile/*"

The ^ anchors the text to the beginning of your HTTP_HOST which is www.example.com So ^.net means your URL must start with any 1 character . and then the word net. I'm going to assume you meant you wanted to match the literal ., in order to do that you have to escape it as \.

You also have to escape the / to \/ and also * at the end of a slash just means match unlimited slashes, but you want it to match any character after the slash unlimited times, so it's .*

So let's rewrite that pattern as we can't use the ^ because your website don't start with .net

pattern="\.net\/profile\/.*"

That will give you this..

  <add input="{HTTP_HOST}{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="\.net\/profile\/.*">

But do you really need the {HTTP_HOST}? It really depends if this is a global rule, if it's not a global rule, and it's at the application level. Then you don't need it.

Now you can add back in the ^ to anchor the word profile to the first part of the REQUEST_URI like this.

<add input="{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="^profile\/.*">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.