Rewrite not working on IIS like I'd expect - iis-7

I have the following redirect
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="a/(.*)/(.*)" />
<action type="Rewrite" url="https://s3.amazonaws.com/bucket/subfolder/{R:1}/{R:2}.o8d" />
</rule>
</rules>
What I am trying to do is I have a url http://www.mysite.com/a/username/filename.
I want that to rewrite to https://s3.amazonaws.com/bucket/subfolder/username/filename.o8d
Currently I get a 404 error when visiting the url's I expect to work.
Any ideas?
Ok an update. I've tried changing <action type="Rewrite" url="https://s3.amazonaws.com/bucket/subfolder/{R:1}/{R:2}.o8d" /> to <action type="Rewrite" url="/" /> and it rewrites to the root of my site just fine. I have arr installed, and I repaired and restarted. Not sure what to do here. Everything I keep reading says this should be doable, but it isn't working.
It's like the rewritter is automatically setting the rewrite to http://www.mysite.com/{Rewrite URL}

Related

ASP.net URL Rewrite subdirectory to external URL

I need to URL Rewrite a subdirectory to an external domain.
Example: When visiting "https://example1.com/test", "https://example2.com/hello" should open. The URL should still be "https://example1.com/test".
I tried to solve this by adding a Rewrite rule in web.config, but it don't works. Here is the Rewrite rule I made:
<rule name="TestRule" stopProcessing="true">
<match url="^test(/.*)?$" />
<action type="Rewrite" url="https://example2.com/hello" appendQueryString="false" />
</rule>
In order to redirect the incoming request to another domain by using Rewrite action type (stay the old URL in the browser address bar), we need to install Application Request Routing module.
https://www.iis.net/downloads/microsoft/application-request-routing
By default, Rewrite action only forwards these requests to the same domain, therefore, we only can specify a URL path in the Rewrite URL field.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#rewrite-action
Otherwise, Redirecting the incoming request to another domain will cause a 404 error.
After we installed the ARR extension, enable Reverse Proxy functionality following the below steps.
Here is an official example.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing
Finally, please refer to the below configuration.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Rewrite" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
It will redirect the requests that has “/test” segment to the https://www.bing.com/maps.
If we want to complete the task without installing ARR extension, we have to use Redirect action type. The URL in the browser address bar will change while we access the qualified URL.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Redirect" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there is anything I can help with.

Why is the cache busting rule declared in web.config being ignored?

I'm trying to use the rewrite rule from the HTML 5 Boilerplate project to make circumventing browser cache (aka cache busting):
<rewrite>
<rules>
<rule name="Cachebusting">
<match url="^(.+)\.\d+(\.(js|css|png|jpg|gif)$)" />
<action type="Rewrite" url="{R:1}{R:2}" />
</rule>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://chewsy.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If i try to access my css with /css/all.123456.css, it fails to find the file with the error reporting that it's looking for /css/all.123456.css (no rewrite). I've tried commenting out the "Remove WWW" rule to see if that was a conflict, but same behavior.
Any ideas why this rule isn't being applied and rewriting the URLs?
Update: I'm using these settings for my web server in VS2010:
<match url="^(.+)\.\d+\.(js|css|png|jpg|gif)$" />
<action type="Rewrite" url="{R:1}.{R:2}" />
I pressume you want to get /css/all.css, if not, post the desired result...
EDIT: VS internal development server (Cassini) doesn't support IIS URL Rewriting Module, you would have to use IIS (Express) for that, or some third party component (http://urlrewriter.net/)...

IIS: web.config redirect to another url preserving the original one in the address bar

I'm trying to understand how to do this, but seems very complicated (at least for someone like me having no experience with IIS but only Apache). I'm porting a website from Linux to a Windows server, and on linux server I have an .htaccess that helps me rewriting urls to hide pages and arguments:
RewriteRule ^store/([0-9a-zA-Z]+)$ http://www.domain.com/store/$1/ [R]
RewriteRule ^store/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name=$1
RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ http://www.domain.com/store/$1/$2/ [R]
RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name=$1&page=$2
So that when someone visits http://www.domain.com/store/client1/ is visiting http://www.domain.com/pages/store.asp?name=client1 (and so on with up to 4 arguments), but in the browser addressbar the url shown is still http://www.domain.com/store/client1/
I can't find my way to do the same on IIS 7...I made something like below:
<rule name="Rule 5">
<match url="^store/([0-9a-zA-Z]+)$" ignoreCase="false" />
<action type="Redirect" url="http://www.domain.com/store/{R:1}/" redirectType="Found" />
</rule>
<rule name="Rule 6">
<match url="^store/([0-9a-zA-Z]+)/$" ignoreCase="false" />
<action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}" appendQueryString="false" redirectType="Found" />
</rule>
<rule name="Rule 7">
<match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$" ignoreCase="false" />
<action type="Redirect" url="http://www.domain.com/store/{R:1}/{R:2}/" redirectType="Found" />
</rule>
<rule name="Rule 8">
<match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$" ignoreCase="false" />
<action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}&page={R:2}" appendQueryString="false" redirectType="Found" />
</rule>
This works for redirecting, so if I call www.domain.com/store/arg1/arg2/ I'm visiting www.domain.com/pages/store.asp?name={R:1}&page={R:2} , but in the browser addressbar I see the redirected address store.asp?name={R:1}&page={R:2} instead of the original www.domain.com/store/arg1/arg2/ , that instead what I need.
Is there a way to do this? I spent already several hours without a working solution...
Instead of using an action of type Redirect, use Rewrite. See this blog post for more information. This will rewrite the URL on the server to the one that you desire instead of redirecting the browser to a new URL.

How can I correctly rewrite a URL with .aspx to a URL without .aspx?

I have a site that currently uses the .aspx extension on its pages. It's getting a Joomla conversion and the .aspx extension will not work anymore. I need it so that if someone enters the .aspx extension, it will just get removed the URL so none of the SEO on the current site breaks.
For example, I need
www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx
to be rewritten/redirected to
www.arrc.com.php5-17.websitetestlink.com/services/managed-services
This is what I have in my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The first URL match is for any URLs that have a URL like services.aspx instead of services/managed-services.aspx
Whenever I go to www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx it gives me an internal server error, but www.arrc.com.php5-17.websitetestlink.com/services.aspx rewrites correctly. What can I do to fix this?
They are greedy, switch the order.
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
Two potential issues:
I think for rewriting, it's going to hit them in order, which means the first rule is going to always kick off. Try switching the order of the rule.
The dash should work correctly in [_0-9a-z-], but try escaping the dash.

Redirect from Default.aspx to root using IIS7

When request comes to www.example.com/default.aspx, I want it to 301 to www.example.com. How to do that?
PS - I tried many rules but nothing seems to work. Thanks.
Have you tried using URL Rewrite with the following rule:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

Resources