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>
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\/.*">
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.