IIS7 Rewrite issue - Multiple websites - iis-7

Hi have an issue with the rewrite mod for IIS7
I have several websites in this example (seasons.com. summer.com, winter.com, spring.com)
All of these domains point to the same folder (g:\websites\seasons\htdocs)
Now the complicated bit.
Summer.com gets rewritten to g:\websites\seasons\htdocs\domains\summer\ so any file in this folder would appear like http://www.summer.com/helloworld.php. This rewrite appears in the g:\websites\seasons\htdocs\web.config
Now summer.com also has its own rewrite rules contained in a web.config file located in g:\websites\seasons\htdocs\domains\summer\web.config
If a visitor attempts to visit http://summer.com/news/
the rewrite moudle rewrites should rewrite to the news.php file contained within the summer folder (g:\websites\seasons\htdocs\domains\summer\news.php) but instead it rewrites to
url: http://www.summer.com:80/domains/summer/news/
path: g:\websites\seasons\htdocs\domains\summer\news\
Here are the rewrite rules
The file located at g:\websites\seasons\htdocs\web.config
<rule name="summer">
<match url="(.*)" />
conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="summer.com" />
</conditions>
<action type="Rewrite" url="/domains/summer/{R:0}" />
</rule>
the file located at g:\websites\seasons\htdcoc\domains\summer\web.config
<rule name="Rewrite to news">
<match url="^news/" />
<action type="Rewrite" url="news.php" />
</rule>
I know this isn't perfect. Any help appreciated

Related

IIS 10 site needing URL Rewrite for changed WordPress subfolder name

I have a client with an ASP.NET MVC project running on IIS 10.
In IIS 10 I have an Application setup under the site that points to a WordPress installation folder. The alias was previously oldblog and has now changed to blog.
The WordPress URI was requested to be changed so I need to permanently redirect requests to the old URI to the new one.
Root ASP site: https://www.example.com/
Old WP URI: https://www.example.com/oldblog/
New WP URI: https://www.example.com/blog/
The following rule only partially works. oldblog does get rewritten to blog, but the rest of the URI is unpredictable/inconsistent.
<rule name="Rewrite /oldblog to /blog" stopProcessing="true">
<match url="^oldblog(.*)$" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/blog/{R:1}" redirectType="Permanent" />
</rule>
I need to rewrite all requests to /oldblog/* to /blog/* - regardless of the full URI/querystring, I only want to rewrite oldblog to blog
/oldblog/ => /blog/
/oldblog/category/somecategory/ => /blog/category/somecategory/
I would suggest you try to make tests with the URL Rewrite rule below could help you redirect from URLs below.
/oldblog/ => /blog/
/oldblog/category/somecategory/ => /blog/category/somecategory/
Sample Rule
<rewrite>
<rules>
<clear />
<rule name="Rule-3" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{REQUEST_URI}" pattern="/oldblog(.*)|/oldblog" />
</conditions>
<action type="Redirect" url="/blog{C:1}" />
</rule>
</rules>
</rewrite>
Test Result
Further, you could modify the rule above as per your own requirements.

IIS URL Rewrite redirect all subdirectories

My website has a lot of old pages and directories not in use. Instead of deleting them, I'm thinking about using the rewrite rules to redirect them to its parent directory,
For example, I have these links:
https://www.example.com/category/sub/1
https://www.example.com/category/sub2222/1/a
https://www.example.com/category/something/sub/3333
I want all three of them to redirect to https://www.example.com/category.
I tried the code below but got into an infinite loop. Guess I have to specify the rule do redirect ONLY when it has a subdirectory:
<rule name="Redirect-test" stopProcessing="true">
<match url="category/(.*)" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/category" />
</rule>
ok, maybe I was overthinking of this, adding a /(.*) would solve my problem, I'm just wondering if there's any flaws in such rule
<rule name="Redirect-test" stopProcessing="true">
<match url="category/(.*)/(.*)" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/category" />
</rule>

IIS 7.0 URL REWRITE

I am setting up a redirect to WWW for one of our sites in the web.config and ran into a small issue. The code I have in the web.config for the rewrite is as follows :
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
I'm finding that it's actually working a little too well. Because of the pattern "example.com", I'm seeing that it's now redirecting to our live site on dev and staging because our URLS are laid out like so : dev.example.com & staging.example.com. For the time being, I have just commented out the rewrite on these other web.configs but I'm wondering if there's a better pattern or option to get around this issue.
If you only want the root domain without subdomains then you should edit your pattern in the HTTP_POST section.
Place a ^ in front of the pattern which means start with. So If the url starts with example.com then it gets redirected to www.example.com.
If its dev.example.com, this rule will be ignored.
Edit your example:
<add input="{HTTP_HOST}" pattern="^example.com" />

IIS7 URL Rewrite ignore rules in sub application

I have MyWebSite and MyApp as shown below:
MyWebSite
web.config
MyApp
web.config
Pages
page.aspx
I want the next URL will work:
www.mywebsite.com/oldpage.aspx
MyWebSite\web.config add to the URL 'MyApp': www.mywebsite.com/MyApp/page.aspx
MyApp\web.config add to the URL 'Pages': www.mywebsite.com/MyApp/Pages/page.aspx
I want to separate this logic in two rules because I want each rule will responsible for its part. MyWebSite should decide which application is default, if not specified, and it should not know anything about 'Pages' in MyApp.
The problem, that the rule in MyApp is not called.
The question: do I do something wrong? Is this is limitation that I can't perform rewrites in separate config files?
P.S. MyWebSite and MyApp running under the same app pool.
MyWebSite web.config:
<rule name="Add 'MyWebSite' for ASPX pages">
<match url="^([^/]+\.aspx)" />
<action type="Rewrite" url="MyApp/{R:1}" />
</rule>
MyApp web.config
<rule name="Add 'Pages'">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="Pages" negate="true" />
</conditions>
<action type="Rewrite" url="Pages/{R:0}" appendQueryString="true" />
</rule>
What you want to do seems possible reading the following blog post: http://www.iis.net/learn/extensions/url-rewrite-module/using-global-and-distributed-rewrite-rules
However, the only way I succeeded in getting it to work was using a dirty trick. I am not sure it would be recommended in a production environment but in case you want to go with this solution:
MyWebSite web.config:
<rule name="Add 'MyWebSite' for ASPX pages">
<match url="^([^/]+\.aspx)" />
<action type="Rewrite" url="http://www.mywebsite.com/MyApp/{R:1}" />
</rule>
I just changed the Rewrite destination to url="http://www.mywebsite.com/MyApp/{R:1}" (I have ARR up and running as well).

Rewrite Subfolder to Subdomain in web.config

I'm attempting to write a rewrite rule for the following scenario.
User attempts to load this picture:
domain.com/images/folder/picture.jpg
and instead, I need it to load:
cdn.domain.com/images/folder/picture.jpg.
Here's what I have that isn't working:
<rule name="CDN rewrite for Images">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
<add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
</conditions>
<action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>
UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...
UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.
EDIT:
To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.
To do so:
Download and install the module
Open your IIS management console (inetmgr)
Select Your server node
Double click on Application Request Routing Cache:
Click on Server Proxy Settings on the Actions pane (right of the screen)
Check the box Enable proxy and click on Apply
The second step is about setting up your rules.
If you want your rewrite to be based on the path then use the following code:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
</rule>
</rules>
</rewrite>
Or if you keep the same folder architecture on the second website you can simplify as follow:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
If you want to catch only the files ending with a specific extension (let's say images):
<rewrite>
<rules>
<rule name="Forward to cdn domain">
<match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool
NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.
<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>

Resources