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
Related
I have my site that uses web.config to set url rewriting.
I cannot change the match url but I want to change (in example for odd days and for even days) the url for the action.
In particular:
<rule name="ricerca21pSEONew" stopProcessing="true">
<match url="^([_A-Z0-9a-z-=%]+)/p([0-9]+)[\/]?$" />
<action type="Rewrite" url="new-sitesearch.aspx?idprestaz={R:2}&prov=domicilio&base={R:1}" appendQueryString="true" />
</rule>
should change url to new-sitesearch-speed.aspx for even days
How to achieve that?
I don't think conditions can be placed in the web.config file...
What I would do is create 2 web.config files:
one with rule "new-sitesearch.aspx"
one with rule "new-sitesearch-speed.aspx
Then create a batch that goes to copy / replace the web.config via Windows scheduled tasks.
Otherwise you should program a single aspx page with if statements based on the date
OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.
Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect. I think I actually DO have the regex correct for this, but it's implementing it in my web.config that is causing the problem.
so here is the input, the URL that a user may access would look like:
https://of.example.com/sfsv3.aspx?waform=pro&language=en
I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:
http://example.com/ca/en/ppf
(where en is replaced by whatever i captured above)
So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:
https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))
so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.
I get the following
{R:1} language=en
{R:2} en
great! so then my IIS rewrite rule should look like this to redirect the user:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
</system.webServer>
this all seems right to me. however, IIS is balking and saying that my web.config is invalid. specifically, it says "Entity '(' is not defined" and it's pointing to the ( right before language as the problem. So, I can't build or deploy this application till I fix that. If i build it without and then just try to drop that into the web.config, i get an error loading the site:
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Can someone help me figure out how to put a capture group into a rewrite rule properly?
You forgot to code the ampersand. Should be:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
I'm trying to set a canonical default URL in IIS 7 using the URL Rewrite module. I think that I'm misunderstanding how the 'Match URL' field is used. The following doesn't seem to do anything:
<rewrite>
<rules>
<rule name="EnforceDefaultPage">
<match url="^http://(?:www\.)?mydomain\.com(?:/)?(?:blog\.aspx)?$" />
<action type="Redirect" url="http://www.mydomain.com/blog" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I've noticed in a lot of examples that people have added a condition utilizing the HTTP_HOST variable... but how does this relate to the match url? It seems that I should be able to omit any conditions because my regex matches exactly what I want.
Ahh, I've finally figured it out. Apparently how 'much' of the URL is available for matching depends on the location of the web.config in the directory hierarchy. Since I was placing the code in the web.config in the web root, it could only match anything after the domain name (i.e. it can match everything after 'blog.com/' in 'http://www.blog.com/').
I found the answer here: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
"Note that the input URL string passed to a distributed rule is always relative to the location of the Web.config file where the rule is defined. For example, if a request is made for http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule is defined in the /content directory, then the rule gets this URL string default.aspx as an input."
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>
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.