URL ReWrite with un-named parameter - redirect

I would like this URL
http://alpha.Mysite.io/Car/Details/{4f2a95ed-3582-4486-8ef6-8a8e6731161f}
to redirect to
http://alpha.Mysite.io/Car/index.htm?Action=Details&guid={4f2a95ed-3582-4486-8ef6-8a8e6731161f}
The GUID parameter ({4f2a95ed-3582-4486-8ef6-8a8e6731161f}) is differenet each time so needs to be passed to the redirected URL.
I have the basic redirect working using a ReWriteMap, however as soon as I add the nameless GUID paramter I get a 404 instead
Here's my section from webconfig
<rewrite>
<rules>
<rule name="Rewrite rule1 for carAPI">
<match url=".*" />
<conditions>
<add input="{carAPI:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="carAPI">
<add key="/car/details/" value="/car/index.asp?Action=Details" />
</rewriteMap>
</rewriteMaps>
</rewrite>

In your case, you do not need rewrite map. You can achieve everything with single rule:
<rule name="Rewrite rule1 for carAPI" enabled="true" stopProcessing="true">
<match url="^Car/Details/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})" />
<action type="Rewrite" url="/Car/index.htm?Action=Details&guid={R:1}" appendQueryString="false" />
</rule>
This rule will match urls like Car/Details/{any valid .net guid} and rewrite them into Car/index.htm?Action=Details&guid={guid}

Related

conditional redirect in web.config

I have a web.config redirect section that is intended to redirect HTTP requests to HTTPS. Here is the redirect section in my web.config file:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add the STS header in HTTPS responses">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
I am not sure what flexibility can be implemented but I want this section to work as-is EXCEPT for a specific URL. Specifically, I want all HTTP traffic to continue to be redirect from HTTP to HTTPS EXCEPT when the request is HTTP://{HOST}/symphony/default.aspx.
Is this possible?
You can try adding a condition to the rule to match the request HTTP://{HOST}/symphony/default.aspx, and then use the negate attribute to negate the result of matching.
More information about negate you can refer to this link: Rule pattern properties.

How to redirect all traffic to https & non-www in web.config?

I'm using an SSL certificate and IIS. In my web.config I want to redirect all website traffic in the following order:
all http --> https
all www --> non-www
You want to use the redirect module in order to do this. This intercepts the request as it's coming in to the server and changes it according to your instructions.
Here is an action to redirect all to https:
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
and here is one to redirect www to non-www:
<conditions> <add input=”{HTTP_HOST}” pattern=”^example\.com$” negate=”true” /> </conditions> <action type=”Redirect” url=”http://example.com/{R:1}” />
There are plenty of articles around the web and StackOverflow when you include both "web.config" and "redirect" in your search.
Make sure that when you set up the rules you only use stopProcessing="true" on the final rule. If that appears on the first rule then the second rule will never execute.
i find the answer
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yoursit\.com$" />
</conditions>
<action type="Redirect" url="http://yoursite.com/{R:1}" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<system.webServer />

How to add RedirectType to External config file in asp.net

I have a separate .config file in the application root directory which contains Mapped URLS for redirect and referenced this .config file in web.config for 301 Permanent Redirect! This works fine.
See Reference Link
Now, i also want to add some links which will redirected as 302 status code. How to add 302 redirect in external .config file and redirect accordingly.
rewritemaps.config
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
Can we specify the Redirect Type i.e 301/302 in this file?
web.config
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config">
</rewriteMaps>
<rules>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
NOTE:Currently all links from file 'rewritemaps.config' are set to 301 Status in web.config.
Can we add as following in rewritemaps.config and redirect accordingly:
<add key="/oldcellphone" value="/newcellphones.aspx" [RedirectType=301] />
<add key="/oldphone" value="/newphones.aspx" [RedirectType=302] />
There are about 1000 links of 301 Status and about 400 links for 302 Status. If its not possible in external file(rewritemaps.config) then please suggest preferred way to do?
Update:
Can you help me to redirect to another site(different domain) if specific string match in requested URL .
Eg: if the requested URL contains "/hm1" then redirect to different site. i.e http://www.google.com
Web.config
<rule name="othersite" stopProcessing="true">
<match url="^/hm1$" />
<action type="Redirect" url="http://www.google.com" redirectType="Found"/>
</rule>
.aspx
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="/hm1">other site (http://www.google.com)</asp:HyperLink>
Can you add RedirectType into a Rewrite Map? No, unfortunately not.
To achieve what you're trying to do you're going to need to create two Rewrite Maps and two Rewrite Rules - one for 301 redirects and one for 302 redirects.
Here's an example of how that might look:
<rewrite>
<rules>
<rule name="301Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
<conditions>
<add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
<rule name="302Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Found" />
<conditions>
<add input="{302Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="301Redirects">
<add key="/oldurl" value="/newurl" />
</rewriteMap>
<rewriteMap name="302Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrite>

iis url redirect http to non www https

i need to redirect from
www.domain.de to https://domain.de
-works
http://www.domain.de to https://domain.de
-works
http://domain.de to https://domain.de
-does not work
rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.
This is the pattern:
"^(www\.)?(.*)$"
where:
{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Here's the rule in full:
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The accepted answer doesn't handle the special case https://www.domain.de.
These rules do the complete job:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you want to redirect www to non www:
Add DNS entry for www.yourdomain.com to refer to your server's public IP
Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
Add a rewrite rule to your website using iis:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.
If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.
This is what worked for me:
<rule name="NameRule1" stopProcessing="true" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>
I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704
Redirecting https://www.example.com to https://example.com I had to do 2 steps
Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}
I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)
After doing both steps only then does it work as desired
Tried many of solutions but below works for me:
Right click on Domain and Add a Site bindings, add one more domain with www:
2. Restart services. it should work.
These rewrite rules matches the following URL's:
www.example.com
http://www.example.com
https://www.example.com
They will all redirect to: https://example.com
These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS 7 rewrite rule - redirect based on presence of querystring

Is it possible to redirect using web.config based on the presence of a querystring in the initially requested URL? I'm not sure what to include in the the conditions. I'm a beginner at working with rewrite/redirect rules in web.config and would like to learn more about syntax, parameters etc.
I'm trying to do something like this:
<rewrite>
<rules>
<rule name="Restricted Folder with Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to a certain page if a certain querystring is present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="/test folder/{R:1}" />
</rule>
<rule name="Restricted Folder without Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to another page if querystring is not present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.whatever.com/page.asp?url={R:1}" />
</rule>
</rules>
</rewrite>
deeholzman, I believe for your Condition - Add Input example you want to use "pattern" and not "matchType". EX:
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
or
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
I found the answer in the URL Rewrite Module Configuration Reference. In the rewrite section of web.config in whatever folder you wanted to perform the redirect from, it would go something like this:
<rules>
<rule name="Restricted Folder Gimbal" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.whatever.com/file.html" />
</rule>
</rules>
For a querystring not present, you could add the 'negate' parameter to the condition:
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
The the URL Rewrite Module Configuration Reference is a very handy reference that unfortunately took me longer than expected to find.