IIS Url Rewrite Rule - iis-7

I am new to Url rewrite rules so any help would be gratefully appreciated.
Looking to create a IIS Url rewrite that will handle any of the following
http://localhost/homepage/contact-us.aspx -> http://localhost/contact-us.aspx
http://localhost/homepage/about-us.aspx -> http://localhost/about-us.aspx
Any ideas?

I haven't tested this but I think you would have something like this in your web.config file.
You can edit the web.config file and then go into IIS and the rule should show up if you want to test it.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewriterule1" enabled="true" stopProcessing="false">
<match url="homepage/(.*)" />
<action type="Rewrite" url="http://localhost/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

web.config redirection ignored on a shared hosting

I have a webpage hosted on a shared Microsoft-IIS/8.5 server.
I don't have the admin privileges on that server, and I don't have access to its configuration files, so I can't really make sure that the URL rewrite module or the HTTP redirect element are correctly installed / set up.
I would like to establish a redirection from a folder named old_folder to a folder named new_folder (both at the root for me, i.e., at http://sharedhosting.com/myusername/).
I placed in the root folder the following web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<sectionGroup name="system.webServer">
<sectionGroup name="rewrite">
<section name="rules" overrideModeDefault="Allow" />
</sectionGroup>
</sectionGroup>
</configSections>
<system.webServer>
<rewrite>
<rules>
<rule name="attempt" stopProcessing="true" enabled="true">
<match url="^old_folder/$" />
<action type="Redirect" url="new_folder/" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But all I can get is a 403 - Forbidden: Access is denied. (if I leave an empty old_folder) or a 404 - File or directory not found. (if I erase it).
I tried using the HTTP Redirect, by placing in old_folder a Web.config file containing
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://sharedhosting.com/myusername/new_folder/" />
</system.webServer>
</configuration>
But that didn't worked either.
I know that my Web.config files are read (I can get 500 errors if there is an error in them). I suspect that the URL rewrite is installed, since, for instance, http://sharedhosting.com/myusername/folder is rewritten to http://sharedhosting.com/myusername/folder/ (i.e., with a slash) if the folder folder exists.
Is my rule correct? Does my host prevent me from redirecting? If yes, how can I tell?
I added a <clear /> after the <rules> tag to discard all the other rewriting rules, but it still isn't redirecting anything.
What am I missing?
LAST EDIT BEFORE THE BOUNTY EXPIRES
To clarify, my question is "How to understand why the server isn't correctly interpreting / ignoring my instructions?".
We came to the conclusion that it was probably my server that was weirdly set-up, I'm precisely trying to "revert-engineer" that and to detect what's making the server ignore the redirect / rewrite rules.
You can achieve the same thing by using the HttpHandlers , so i'm presuming that you can access the both Old_folder and New_folder directly in the browser .
Here is a sample httphandlers , you can do in this way :
youname.Handlers
{
public class RedirectHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Status = "301 Moved Permanently";
string url = context.Request.Url.ToString().Replace("old_folder", "new_folder");
context.Response.AddHeader("Location", url);
}
public bool IsReusable
{
get { return false; }
}
}
}
In web_config add the handler as follows:
system.web>
<httpHandlers>
<add verb="*" path="xxx/*.*" type="yourname.Handlers.RedirectHandler"/>
</httpHandlers>
</system.web>
Using IIS Url_rewriting :
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="yourRedirects">
<add key="/yourroot/old_folder/" value="/otherdir/new_folder" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{yourRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I think this can be done with a simple rewrite rule.
<rule name="ToNewFolder" stopProcessing="true">
<match url="^old_folder/(.*)" ignoreCase="true" />
<action type="Rewrite" url="new_folder/{R:1}" />
</rule>
The above rule will send all requests to the new folder, but keeps the old url with old_folder in it. If you want the url to change it's display to new_folder in it, use
<rule name="ToNewFolder" stopProcessing="true">
<match url="^old_folder/(.*)" ignoreCase="true" />
<action type="Redirect" url="new_folder/{R:1}" />
</rule>
Tested it on my local machine with folders, subfolders an requests with a QueryString. I hope this works on shared hosting also.

Windows server not displaying image and css after web.config change

I transferred a website from a linux host to windows server. When I changed http access rules in the web.config; images and css are not displayed on web.
Web.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rule 1R" stopProcessing="true">
<match url="^(.*)$" />
<action type="Rewrite" url="/index.php/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
And my website url is : http://www.magazzinopneumatici.it/

How to point urls to certain pages with Web.config?

I have never written a Web.config file before and to note I cannot use .htaccess. What I am trying to do is fairly easy I believe. I am trying to point a set of URLs to certain pages and/or crossed with setting up vanity URLs. Below are the examples I need:
www.mysite.com/test - this fails to go to www.mysite.com/test/ the slash on the end makes it work. So I want it to work without it.
www.mysite.com/test | get it to work
www.mysite.com/test/index.aspx | goes to | www.mysite.com/test
www.mysite.com/test/page2.aspx | goes to | www.mysite.com/page2 (only the page exist - there is no folder - Like a vanity if possible)
Not sure how to write this, but was trying...
<configuration>
<location path="index.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="www.mysite.com/test" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
Sounds like you're looking to rewrite the url. I'm not sue I fully understand what you're after, but if should be pretty simple after looking at a few examples. For instance:
/test/index.aspx -> /test/ could be written as:
<system.webServer>
<rewrite>
<rules>
<!-- /test/index.aspx to /test/ -->
<rule name="test index" stopProcessing="true">
<match url="^test\/index\.aspx$" />
<action type="Rewrite" url="/test/" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you prefer a 301 Permanent Redirect change type="Redirect" instead of Rewrite.
Likewise for specific pages:
<system.webServer>
<rewrite>
<rules>
<!-- /test/page1.aspx to /page1 -->
<rule name="test pages" stopProcessing="true">
<match url="^test/(page\d+)\.aspx$" />
<action type="Rewrite" url="/{R:1}" /> <!-- {r:1} is the "PageN" portion -->
</rule>
</rules>
</rewrite>
</system.webServer>

IIS 7 url rewrite module on web.config

can anybody help me with this please. I have IIS7 and the hosting says that the URL Rewrite module has been installed already. I even put into the bin directory the Microsoft.Web.Iis.Rewrite.dll
i can only use the web.config for the url rewrite configuration since I dont have access to the IIS manager (hosting restriction).
here is my code on the web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to list_cities.aspx">
<match url="^/state/([a-zA-Z]+)" />
<action type="Rewrite" url="~/list_cities.aspx?state={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
I still get the error, HTTP Error 404.0 - Not Found, when i go to
http://xxxxxxxx.com/state/CA/ . I have already searched the issue but I cant seem to find any solutions.
Can anybody please check my code. Thanks a lot.
I found the solution just now (not being an IIS expert...)
Change {R:1} for {R:0}
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to list_cities.aspx">
<match url="^/state/([a-zA-Z]+)" />
<action type="Rewrite" url="~/list_cities.aspx?state={R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Simple IIS7 Url Rewrite doesnt work

at first. I have searched and read here a lot and googling with bing but didnt find
the solution. In my local enviroment it just works. Iam no IIS Admin so ....
i try the following
My domain "http://mysite.com/" goes to my url provided by my hosting service
(discountasp.net)
I want that this url goes to the root/mysite/ virtual directory but i want that the
url stays on "http://mysite.com/". So i defined the following rule, but it doesnt
work for me.
here is my web.config that is placed in the root directory (generated by the IIS7
Remote Administration UI)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="mysite">
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="mysite.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mysite.com" />
</conditions>
<action type="Rewrite" url="\mysite\{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The "#Html.ActionLink" creates a link with the virtual directory
"http://mysite.com/mysite/"
I got it. Pointing my Domain directly on the subfolder and defining a Outbound rewrite rule on iis solves my problem.

Resources