I have some external links coming into my site which mistakenly had a period added to their ends. (so, I can't fix them). Since inbound links are always good, I want to redirect these links to a legit page. I've tried a number of rules in the urlrewrite module for iis.5 and nothing seems to capture this url.
I've seen other questions on here regarding asp.net urls with periods at the end, but I'm trying to capture this one at the IIS rewrite module level. Any pointers on making this work?
This is quite an old one but as i was facing similar issue recently i've decided to post my findings...
I assume that one of modules that run prior to url-rewrite module terminates, with an exception, preventing request from continuing to url-rewrite (i suspect some security checks). Thus it is not possible to resolve your issue with url-rewrite.
One possible workaround could be to redirect requests ending with dot '.' at the very early stage in Global.asax, like here:
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
if (Context.Request.RawUrl.EndsWith(".aspx."))
{
//permanent redirect and end current request...
Context.Response.RedirectPermanent(Context.Request.RawUrl.TrimEnd('.'), true);
}
}
That might be far from optimal answer but at least it gets job done ;)
I used a solution that removes all trailing dots at the web.config level:
<rule name="RemoveTrailingDots" stopProcessing="false">
<match url="^(.*[^.])\.+$" />
<action type="Rewrite" url="{R:1}" />
</rule>
This rule just rewrites the request url internally at the beginning of my rewrite rules, allowing the request to process through the normal rules instead of being caught upstream
If you don't have any more rules to apply, and just want to redirects anything with dots at the end, just change the action type to Redirect (case sensitive)
If you have a fixed set of inbound links that are incorrect, you can concentrate on redirecting the main part of the URL, rather than the dot.
For example, with this URL:
http://www.example.com/index.html.
Have your IIS Rewrite look for a URL matching "^index.html.*" and redirect to the page you need. That should catch the URL both with and without the dot.
On IIS 8.5 I implemented something substantially similar to that proposed by GWR but with one subtle change. I needed the attribute appendQueryString="true" on the Rewrite action tag. (Perhaps this was the default back in 2016, but now needs to be specified.)
Here's what worked for me:
<system.webServer>
<modules>
...
</modules>
<validation validateIntegratedModeConfiguration="false" />
<rewrite>
<rules>
<rule name="RemoveTrailingDots">
<match url="^(.*[^.])\.+$" />
<action type="Rewrite" url="{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
I had to install the URL Rewrite feature from here: https://www.iis.net/downloads/microsoft/url-rewrite
Related
I'm here in last resort. So here is my problem,
At work my boss asked me to rewrite an URL to be more user friendly.
The old pattern is this one: https://{hostServer}/SiteName/index.html
The new one should be this one: https://maps.swcs.be
But i looked in the doc of IIS, looked in here for some more informations etc...
I learned, it's done in the web.config file at the root of the repo "wwwroot" So I wrote my rule in there
here it is
<system.webServer>
<rule name="redirection d'url">
<match url=".*"/>
<conditions>
<add input="{https://maps.swcs.be}" type="Pattern" pattern="Ëxpertises[0-9]\index\.html">
</conditions>
<action type="Rewrite" url="https://maps.swcs.be" />
</rule>
</system.webServer>
I tried the <action type="Redirect" ...> too and other stuff but it's the only one that returned something... the IISstart.htm file
I'm pretty new to URL redirection, if one of you could enligthen me that would be awesome
Thanks in advance
First of all, I highly recommend that you read the documentation provided by lex li in detail, which can help you fully understand how to use url rewrite.
Secondly, I want to know your initial url and rewritten url, you don’t have to show the correct url, just an example. Such as initial url is http://example.com/aaa/bbb/ccc/index and rewritten url is http://example.com/index
So I assume your initial url is https://{hostServer}/SiteName/index.html and rewritten url is https://maps.swcs.be.
This rule will match the uri, if the uri is /SiteName/index.html, it will redirect to https://maps.swcs.be. You can also change antion type to rewrite. If you choose rewirte, nothing will happen when you check url in browser. But redirect will make the url change to new one in browser.
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."
Whenever a request
http://localhost:9000/Content/PDF/ABC.pdf
comes (in IIS), i want URL rewrite module to process that request and redirect it to
http://localhost:9000/User/GetPdf
so that my controller and action methods are invoked and my code gets executed before a pdf file is shown to end user.
Here User is UserController and GetPdf is an ActionMethod in my application.
Can any body tell me steps to create this URL rewrite.
Thanks in advance.
I'm assuming you are using IIS 7.x and you have installed URL Rewrite 2.0 module.
In your application's web.config file, add a <rewrite> element similar to:
<system.webServer>
<rewrite>
<rules>
<rule name="PDF Rewrite">
<match url="Content/PDF/([\w-]+)\.pdf" />
<action type="Rewrite" url="User/GetPdf/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
The ([\w-]+) portion of url="Content/PDF/([\w-]+)**\.pdf" will "capture" the name of the PDF ("ABC" in your example) without the file extension. The {R:1} portion of <action type="Rewrite" url="User/GetPdf/{R:1}" logRewrittenUrl="true" /> will then insert the captured string. The net result is that:
http://localhost:9000/Content/PDF/ABC.pdf
becomes:
http://localhost:9000/User/GetPDF/ABC
I am assuming that you will need to pass in the name of the PDF to your Action method, so if you implement the standard routing pattern {Controller}/{Action}/{id}, then the id will be set to "ABC".
The best introduction to the URL Rewrite module that I have found is actually the URL Rewrite 1.1 configuration reference. Even though it is for v.1.1 rather than 2.0, it provides a better overview than the v.2.0 Configuration Reference. It is worth reading from the start. In particular, one must understand the “Accessing URL Parts from a Rewrite Rule” section.
To debug the URL Rewrite module, you can use the IIS 7.x "Failed Request Tracing," which, contrary to its name, can be used to trace successful as well as failed requests. This www.iis.net entry illustrates how to configure this and amount of detailed debugging information that is available.
Also note that the logRewrittenUrl="true" attribute means that the rewritten URL will be logged to the standard IIS log instead of the original URL. (It can't be used to log both the original and the rewritten URL--- you can only get one or the other.)
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>