How to do 307 redirect with tuckey UrlRewriteFilter - tuckey-urlrewrite-filter

I have the following rule...
<rule>
<condition type="scheme" operator="equal">^http$</condition>
<from>/(.*)</from>
<to type="temporary-redirect" last="true">https://something.com/$1</to>
</rule>
... And it works just fine (gives a 302 redirect) however i want to make it 307 redirect since that preserves parameters. I tried the following and have had no luck:...
<rule>
<condition type="scheme" operator="equal">^http$</condition>
<from>/(.*)</from>
<set type="status">307</set>
<to type="temporary-redirect" last="true">https://something.com/$1</to>
</rule>
Any idea how to do this?

Figured it out!... You have to set the location header manually. Also null out the "to" node.
<rule>
<condition type="scheme" operator="equal">^http$</condition>
<from>/(.*)</from>
<set type="status">307</set>
<set type="response-header" name="Location">https://something.com/$1</set>
<to last="true">null</to>
</rule>

Related

What is wrong with this redirect rule?

I'm using the code below to create a redirect rule in my web.config file in the root directory of a site hosted in a Windows server:
<rewrite>
<rules>
<clear />
<rule name="Redirect https" enabled="true" stopProcessing="true" patternSyntax="ECMAScript">
<match url=".*cashpreview\\.com\\.br.*" ignoreCase ="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The rule works for the address http://www.cashpreview.com.br/cashpreview/cashbr.htm but not for
http://www.cashpreview.com.br/cashpreview/janelas.htm . If I try to access the first address, the "https" replace the "http", but the same doesn't occurs for the second address. I need to write the domain in the regular expression because I have multiple domains sharing the same web directory and only this has SSL installed for it. Does somebody knows what could be causing this strange behaviour of the rule?
Now, I've discovered my errors. I was not aware that the parameter url of the match element contains only the part of the url relative to the directory where the web.config file is located. So, the domain never is part of the url. As I have multiple domains sharing the same web directory, I added a new condition checking the {HTTP_HOST} for the only domain for which I've installed SSL. As I have asp files in the site and I used {REQUEST_URI} that include de querystring, I turned off the appendquerystring attribute. My code now is like below:
<rewrite>
<rules>
<clear />
<rule name="Redirect https" enabled="true" stopProcessing="true" patternSyntax="ECMAScript">
<match url=".*" ignoreCase ="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern=".*cashpreview\.com\.br" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
That case where sometimes the things go right was only for data cached in my browser. Using a proxy, that case didn't worked the way I was doing before too.

URL Rewrite same host different port in IIS 8.5

I have a server with three websites:
Default website, port 80 (no content)
Website A, port 81
Website B, port 82
I would like to use URL Rewrite to access Website A and Website B like:
http://myserver/A
http://myserver/B
instead of
http://myserver:81
http://myserver:82
I have installed the URL Rewrite and Application Request Routing modules and enabled the proxy for ARR.
On the default website I created two rewrite rules using the regex pattern /A/(.*) and /B/(.*):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WebsiteA" stopProcessing="true">
<match url="/A/(.*)" />
<action type="Rewrite" url="http://localhost:81/{R:1}" />
</rule>
<rule name="WebsiteB" stopProcessing="true">
<match url="/B/(.*)" />
<action type="Rewrite" url="http://localhost:82/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Using the Test pattern... functionality I could verify that {R:0} contained the entire test URL and {R:1} just the part after A/ or B/. The rewrite URL I sat to point to either port 81 or 82. I still regard regular expressions as pure magic but the test gave me what I wanted. Trying to connect to the websites through /A/ and /B/ gave me 404 errors.
Seeing that this is a very common problem with lots of questions and answers I picked one suggesting to use the regex pattern ^A(.*)$:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WebsiteA" stopProcessing="true">
<match url="^A(.*)$" />
<action type="Rewrite" url="http://localhost:81/{R:1}" />
</rule>
<rule name="WebsiteB" stopProcessing="true">
<match url="^B(.*)$" />
<action type="Rewrite" url="http://localhost:82/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Using the test functionality my test URLs failed to match that pattern, but when I tried to access the sites using the new pattern, it worked.
Question: I am going with the later solution but is it pure luck that it works and should I perhaps do something different?
Is it normal for the test functionality to not successfully match the same URL's that works when I feed them to the server?

check if url has '/?' thru tuckey urlrewrite

I need to check thru URL rewrite if the URL has question mark after 'search-product-list/'. I'm using tuckey. Here's the current rule:
<rule>
<from>^/n/search-product-list/(.*)$</from>
<to last="true" type="permanent-redirect">/ttsvr/search-product-list/$1</to>
</rule>
<rule>
<from>^/search-product-list/(.*)$</from>
<to last="true" type="forward">/n/force-redirect/fitmycar-webdesign-73?query=$1</to>
</rule>
i was able to get the result I expected by doing this:
<rule>
<from>^/search-product-list/(.*)?q(.*)$</from>
<to last="true" type="forward">/n/force-redirect/fitmycar-webdesign-73?query=$1</to>
</rule>

regex redirect "/service" IIS 7

I am new to .NET and IIS ans I am trying do a regular expression redirect with this pattern \/([^\/]+)(?=\/[^\/]+\/?$) so my url wil go from mysite.com/en/solutions/exemple to mysite.com/en/exemple but in the redirection IIS put /Service in my URL someone as a idea how to fix this?
here is the rule generated in my web.config by IIS 7
<rewrite>
<rules>
<rule name="redirect salesforce" stopProcessing="true">
<match url="\/([^\/]+)(?=\/[^\/]+\/?$)" />
<action type="Redirect" url="/en/{R:0}/coveo-for-salesforce" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Thanks
UPDATE:I tried
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)(/[^/]+)(/.*)" />
<action type="Redirect" url="/{R:0}{R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I get a not not found
Please note that I am working on a Sitecore solution with 2 languages en and fr
If you want to remove the first level folder from the url, e.g.:
/en/solutions/example-page -> /en/example-page
/en/other-folder/another-page -> /en/another-page
/fr/one-more-folder/blah-page -> /fr/blah-page
with the 301 redirect code, use the following rule:
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)(/[^/]+)(/.*)" />
<action type="Redirect" url="/{R:1}{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
If you want to redirect with 301 code from mysite.com/en/solutions/exemple to mysite.com/en/exemple (so in fact remove solutions from all urls (but only if they are directly after language), you can use:
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)/solutions(/.*)" />
<action type="Redirect" url="/{R:1}{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I've tested the solution on Sitecore application and it definitely works.

Tuckey Rewrite: Removing Extension

I'm using Tuckey to remove the URL .xhtml extension with the following rules:
<rule>
<from>^/?([a-z-]+)/$</from>
<to>/$1.xhtml</to>
</rule>
<rule>
<from>^/?([a-z-]+)$</from>
<to>/$1.xhtml</to>
</rule>
So /page/ and /page gets mapped to /page.xthml
However, if I have /directory/page/ this doesn't map to /directory/page.xhtml.
<rule>
<from>^/?([a-z-/]+)/$</from>
<to>/$1.xhtml</to>
</rule>
<rule>
<from>^/?([a-z-/]+)$</from>
<to>/$1.xhtml</to>
</rule>
I added the slash into the list of repeatable characters and this worked
use this rule :
<rule>
<from>^/?[a-z-]*?/?([a-z-]+)/$</from>
<to>/$1.xhtml</to>
</rule>
<rule>
<from>^/?[a-z-]*?/?([a-z-]+)$</from>
<to>/$1.xhtml</to>
</rule>