Rewrite url query string to look like directory - azure

Well, I am not good in English actually so I couldn't explain my problem shortly in the title...
Shortly, I need to make
http://<SITE>/user/?id=<x>
to
http://<SITE>/id<x>
Is it possible in Microsoft Azure using web.config?

Inside your web.config you should insert inside <system.webServer> section this rules:
<rewrite>
<rules>
<rule name="Redirect for user" stopProcessing="true">
<match url="^user" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d+)" />
</conditions>
<action type="Redirect" url="/id/{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
It will redirect urls like that http://<SITE>/user/?id=<x> into http://<SITE>/id<x> where <x> is digits

Related

How to add rules in Url redirect in iis 10

I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.

URL Rewrite Redirect

I dont understand writing URL Rewrite aspects yet. But need assistance as I dont know what I am doing incorrectly.
My goal is when a USER clicks on a link
www.example.com/NM/Registration?ctl=PasswordReset&resetToken=db08aa18-0810-417d-a633-131635bf9e8e
I would like the user to be redirected to
www.example.com/Registration?ctl=PasswordReset&resetToken=db08aa18-0810-417d-a633-131635bf9e8e
As you can see I would like to redirect without the NM in the URL path to the root domain.
I have this in my web.config file but it seems that it wont redirect properly:
<rewrite>
<rules>
<rule name="Query String Rewrite">
<match url="/NM/Registration" />
<conditions>
<add input="{QUERY_STRING}" pattern="ctl=([a-z]+)" />
<add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*resetToken=([_0-9a-z-]+)" />
</conditions>
<action type="Redirect" url="http://www.example.com/Registration?ctl={C:1}&resetToken={C:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I figured out my answer. I ended up rewriting the rule so hopefully this will help out anyone in the future.
<rewrite>
<rules>
<rule name="Query String For Redirect">
<match url="NM/Registration" />
<conditions>
<add input="{QUERY_STRING}" pattern="ctl=([a-z]+)&resetToken=([_0-9a-z-]+)" />
</conditions>
<action type="Redirect" url="Registration?ctl={C:1}&resetToken={C:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>

Azure Web App URL Rewrite not working

I'm trying to get a basic URL rewrite to work for my Azure web App
<system.webServer>
<rewrite>
<rules>
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="/SalesCloudGmail.aspx" />
<action type="Redirect" url="/SalesCloudGmail" />
</rule>
</rules>
</rewrite>
</system.webServer>
Was expecting to open the URL .../SalesCloudGmail.aspx and see .../SalesCloudGmail in the Address bar???
What Am I missing
Give this a try, your regex pattern matching is a little off based on how you're using it. But this should work or at least get you much closer.
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^\/SalesCloudGmail\.aspx.*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/SalesCloudGmail" />
</rule>
Please try below rewrite rule:
<rewrite>
<rules>
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="^\/SalesCloudGmail\.aspx$" />
<action type="Redirect" url="/SalesCloudGmail" />
</rule>
</rules>
</rewrite>
Please following this article to test your match pattern.

How to append query string & value via IIS rewrite rule?

I'm running IIS 7 with the offical rewrite rule module installed. I'd like to create a rewrite rule to match this URL:
http://www.sample.com/en-us/test.aspx?q=keyword
After rewriting the expected result would be:
http://www.sample.com/en-us/test.aspx?q=keyword&flag=value
How can I create a rule to implement this?
I've tested the following rule, but no luck, it always got redirect loop error:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)/test\.aspx(.(?!flag=value))*$" />
<action type="Redirect" url="{R:0}&flag=value" appendQueryString="false" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
Found the solution by myself, just share it.
<rewrite>
<rules>
<rule name="Redirect for download result page" stopProcessing="true">
<match url="(.*)/test.aspx(.*)" />
<action type="Redirect" url="{R:1}/test.aspx?rf=sp" appendQueryString="true" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="flag=value" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>

Simple Redirect using Rewrite Module

I want add a rewrite rule to IIS 7 rewrite.
I want to redirect a user from page
http://localhost/myapp/register.html to http://localhost/myapp/register/register.html
And similar for other pages.
Any help?
Can I do the same using a rewrite action?
Using IIS7 and Windows Server 2008.
Thanks.
I'm not sure if it suits your needs but you can try this one (and maybe make some small adjustments if the case):
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="Register Redirect">
<add key="register.html" value="/register/register.html" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Redirect rule1 for Register Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(.*)/register/(.*)(\.html)?$" negate="true" />
</conditions>
<action type="Redirect" url="{HOST}/register/{R:1}" appendQueryString="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>

Resources