How would one redirect from www.example.com/section/index.aspx to www.example.com/section using rewrite rules in web.config? It would also have to work for various levels such as www.example.com/parent/child
*Noting that I do not have access to the server. I can basically just edit the web.config file and tell the server to rebuild the application.
Your best bet is to use the IIS7 URL Rewrite Module - but you would need to install this on the server. It's pretty easy to use and powerful at the same time. It may already be installed if you're hosted, because although it's not installed by default, it is from Microsoft and pretty frequently used.
If you're on 2.0 or greater of asp.net, you can add a urlMappings section to the web.config:
<system.web>
<urlMappings enabled="true">
<add url="~/Section" mappedUrl="~/Section/index.aspx"/>
</arlMappings>
</system.web>
But this has some issues : Firstly, if the URL requested isn't handled by the ASP.Net module, or isn't delivered to your application, the rewrite never happens. This could occur because you aren't hitting a ".aspx" file, for example. Also, in some configurations, the file you request needs to exist. Another issue is that there are no wildcard rules supported, so you would have to add rules to rewrite all possible paths individually.
And finally, there are asp.net rewrite httpmodules you could drop in the bin directory and add to your web.config. Here's some (possibly outdated) options by ScottGu for url rewriting.
This is probably massively disgusting but by creating rules for each possible level I was able to rewrite all paths dropping index.aspx from the url.
starting with
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+).aspx"/>
<action type="Redirect" redirectType="Permanent" url="/"/>
</rule>
and ending with
<rule name="Migrate to PHP all the way">
<match url="^([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+).aspx"/>
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}/{R:3}/{R:4}"/>
</rule>
Related
I am trying to forward a specific URL on my website to another website and I can't figure out how to set the proper values in the web.config file for my IIS server.
For example I would like to forward
www.example.com/ballot
to
ballot.example.com
So far I've tried the following but it doesn't have any affect to the url
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^/ballot$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://ballot.example.com/" />
</rule>
</rules>
</rewrite>
I'm hosting the website on a Rackspace Cloud site.
Okay, I figured this one out and I'm posting the solution here for anyone else that may come across a similar situation.
If you want to redirect a certain URL like http://www.example.com/ballot to http://ballot.example.com/
Then follow these steps
If one doesn't already exist create a directory called ballot directly under the web root of www.example.com.
Create a Web.config file and place it in the ballot directory with the following content.
Make sure you change the destination to your actual destination.
If you are hosting on a Rackspace cloudsite you must rebuild the application (not sure about other host). You can rebuild the application by deleting the Web.Config file (if one was present) and uploading a new one, or by clicking the rebuild application link within the General Settings tab of the cloudsite you are trying to redirect. More information about rebuild applications in Rackspace here: http://www.rackspace.com/knowledge_center/article/how-to-rebuild-an-aspnet-application-in-cloud-sites.
Also here is a link to the page where I ultimately found the solution to what I was trying to accomplish: https://www.stokia.com/support/misc/web-config-response-redirect.aspx
I have had a site at www.mysite.com/myfolder/ for years. I have developed a new version and want to just remove the /myfolder so that the site serves all pages from www.mysite.com.
Example: www.mysite.com/myfolder/mypage.htm will be permanently redirected to www.mysite.com/mypage.htm
How do I configure this using the URL Rewrite module in IIS7? I start with a blank rule, but then get lost. Thanks.
You either use the IIS Management UI or edit the web.config directly. With a section in web.config that looks like this, you will redirect everything (images as well), which might be want you want.
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="^myfolder/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you use the Management tool, you add an empty rule, give it a name, add ^myfolder/(.*) as the pattern, select Redirect as action type, add {R:1} as target URL and select 301 as redirect type.
This is supposed to be permanent, that's why you should use a 301 redirect (for SEO purposes and so on).
I'm trying to rewrite a url:
www.mydomain.com/urlfolder/filename.aspx
to point to
www.mydomain.com/urlfolder/newfile.aspx
My web config has the following:
<rule name="PageRedirection" enabled="true" stopProcessing="true">
<match url="urlfolder/filename.aspx" />
<action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
</rule>
the problem is that this is catching urls such as:
www.mydomain.com/subdirectory/urlfolder/filename.aspx
I tried to change my url to be
but the ^ didn't work. It also seems ~/ doesnt work to specify the root either.
How would I go about specifying this url from the root, w/o putting in an absolute path.
I also have:
testsite.mydomain.com/
and I want the SAME web.config deployed there to work.
Thanks!
I know this question is nearly 4 years old, so you've probably long since moved on from needing an answer.
^ should work though:
<rule name="PageRedirection" enabled="true" stopProcessing="true">
<match url="^urlfolder/filename.aspx" />
<action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
</rule>
^ specifies the beginning of the path string, so whatever you put after the ^ would match only what appears right after the site root and a slash.
e.g.
www.example.com/urlfolder/filename.aspx
would match.
www.example.com/subdirectory/urlfolder/filename.aspx
would NOT match.
The only reason I can think of why it would match /subdirectory/urlfolder/filename.aspx as you say, is that there is a copy of your web.config
in /subdirectory/ in which case www.example.com/subdirectory/ is treated as the root.
If that's the case and your web.config is being copied to places other than your root directory, another option is to add the rule in %windir%\system32\inetsrv\config\ApplicationHost.config instead of a web.config. This is the file that contains all settings that apply to all of IIS on your machine, not just a particular site. In my experience, Visual Studio will refuse to open this file. However, you can edit the file in a different editor, or set the rewrite rules through the IIS GUI.
In the GUI, at the top level of the file tree, the tools available at this level include the URL Rewrite utility, and rules set there write to ApplicationHost.config.
Hope this helps
I'm using ASP.NET 3.5 with IIS 7 with the URL Rewrite Module 2.0 installed.
When I create the first test rewrite rule:
<rewrite>
<rules>
<rule name="Test rule1" patternSyntax="ExactMatch">
<match url="w/123/test" />
<action type="Rewrite" url="article.aspx?id=123" />
</rule>
</rules>
</rewrite>
It works perfectly (http://www.myapp.com/w/123/test -> http://www.myapp.com/article.aspx?id=123).
BUT, when I try to use it on a domain that I own, it doesn't work. I assume that something in my syntax is wrong. Here is what i'm trying to do:
<rule name="Test Rule2" patternSyntax="ExactMatch">
<match url="http://www.my-domain.com" />
<action type="Rewrite" url="article.aspx?id=123" />
</rule>
When I try to browse http://www.my-domain.com I expect to be redirected to the article.aspx page, which I don't, I just get 404 page not found.
How should I write a rule for a domain and not for a path ?
Thanks in advance, Gal.
Rules are relative to the place where the web.config lives. You don't need to specify any domain for your rule. The input for your /> is going to be always the URL Path without the query string and without leading slash. That means, if you request "http://www.my-domain.com/" the input is going to be "". If you request "http://www.my-domain.com/w/123/test", the input would be "w/123/test".
If you just browse to http://www.my-domain.com/" the "Default Document" module in IIS will try to rewrite your request to something like http://www.my-domain.com/default.html" and that won't match your rule. Make sure to disable default document module.
If that doesn't work, URL Rewrite has a tracing feature where you can see step-by-step the workflow:
http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/
N.B. For server rules, the input URL includes leading slash always.
I'm currently planning to deploy a site with a third party hosting provider. I will only have access to the server via ftp and a tool similar to cpanel called WebsitePanel.
No access to IIS set up or configs.
Is there anyway to redirect http://www.example.com to http://example.com?
Place this in your web.config using your values for domain.com. This leverages the URL rewrite rules of the web.config and IIS 7.
<system.webServer> / <rewrite> / <rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
redirectType="Permanent" />
</rule>
Typically, the "tool similar to cpanel" should give you this option.
Failing that, you should be able to:
a) set a custom 404 page pointing, to, say, myredirector.asp [or whatever server-side script you wish to use]
b) in myredirector.asp [or whatever] , do a server-side redirect as appropriate.
Not as clean as a straight IIS redirect, but it works pretty good.
I'd suggest you do this through the domain's DNS configuration, rather than through your application. It's much simpler and doesn't rely on application code to work (so if you deploy a whole new application, you don't have to remember to add any config entries or similar).
Same thing can be done to add the prefix www also. A blog post for the same at following URL:
http://karmic-development.blogspot.in/2013/10/add-prefix-www-automatically-in-url-in.html