I would like create URL rewrite rule that will set default document for my virtual folders. eg. someting like this
www.domain.com/en/ -> www.domain.com/en/index.aspx
www.domain.com/hr/ -> www.domain.com/hr/index.aspx
www.domain.com/de/ -> www.domain.com/de/index.aspx
directories en, hr, de doesn't really exists on web server they are just markers for languange used in site used by home grown http module that will rewrite path with query params.
Quick solution was define rule for every single lang, something like this :
<rewrite>
<rewriteMaps>
<rewriteMap name="Langs">
<add key="/en" value="/en/index.aspx" />
<add key="/hr" value="/hr/index.aspx" />
<add key="/de" value="/de/index.aspx" />
</rewriteMap>
</rewriteMaps>
<rules>
But I would really like solution that would not require changes in web.config and adding rewrite rule for every languange used on particular site.
Thanks !
<rule name="Lang-Redirect">
<match url="^(\w{2})\/?$" />
<action type="Rewrite" url="{R:1}/index.aspx" />
</rule>
That should allow you to capture the language tag from the request and rewrite it to your custom http handler.
Related
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.
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, the redirect is not occurring. it seems to have a problem with the part \? (an escaped question mark to mark the start of the query string). if this is included, then the redirect simply does not happen.
Can you help me figure out how to get it work?
for the record, I figured it out. this is a special case of regex, running it inside a web.config as part of a urlrewrite action. in that case, you can't handle the query string with simple regex, you have to put in conditions on the query string. Here's what eventually ended up working:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true" stopProcessing="true" enabled="true">
<match url="sfsv3.aspx\.aspx" ignoreCase="true"/>
<conditions>
<add input="{QUERY_STRING}" pattern="subject=PROPRCH" />
<add input="{QUERY_STRING}" pattern="(..)/subject" />
</conditions>
<action type="Redirect" url="https://www.example.com/ca/{C:1}/forms/ppf" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
I have many global IIS7 URL Rewrite rules and they by default apply to all sites. Well there are several sites that i would like to disable this rewrite inheritance on for all rules. How can I do this? I tried the following without joy:
<rewrite>
<rules>
<clear />
</rules>
</rewrite>
Sorry, that cannot be done:
Global rewrite rules are used to define server-wide URL rewriting
logic. These rules are defined within applicationHost.config file
and they cannot be overridden or disabled on any lower configuration
levels, such as site or virtual directory. Global rules always
operate on the absolute URL path (that is, requested URI without the
server name).
and
Global rule set is always evaluated first, and after that distributed
rule set will be evaluated by using a URL string produced by global
rule set.
http://learn.iis.net/page.aspx/468/using-global-and-distributed-rewrite-rules/
If you are able to move the functionality you need out of the global rules and into the web.config for your sites, you can add a condition on each rule to either opt-in or opt-out of each rule based on the presence of a local file for that site. This would allow you to deploy a common set of code and config, but with local/per-site customization.
Example of an opt-in rule:
<rule name="HTTP to HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<!-- Opt-in to rule via presense of a local file -->
<add input="{DOCUMENT_ROOT}/Local/rewrite-rule-enable-https.txt" matchType="IsFile" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
Note: The Local folder is not deployed, but rather used to store per-site files.
So essentially this rule does not turn on unless a file called "rewrite-rule-enable-https.txt" is found in the /Local folder on site.
i am currently using IIS7's Url Re-write module, but the main loophole of using the IIS7's url re-write module, is that i have to write rule for all the page,which i want to use on the website, i want to use a comman rule and redirect it to particular page (say homepage) and using global.asax i can redirect it to desired page...
Is it possible with url re-write or is there any tool available i can use for this purpose, or a code sample that could help me doing this.
i dont want extension in the url.
i have pages like index.aspx, news.aspx, artists.aspx, lessons.aspx... i want the urls like index, news, lessons, artists, i created a rule in web.config like
< rewrite>
< rules>
< rule name="urlType1">
< match url="^(\w*)" />
< action type="Rewrite" url="default.aspx" appendQueryString="false" />
< /rule>
< /rules>
< /rewrite>
this will land any page to default.aspx, and then using rawUrl in the global.asax, i am checking for the page like if user has entered "news" then i rewrite to news.aspx
Hope this has helped.
You can do just as you're suggesting in your question--redirect all requests to a single URL:
And then in your Global.asax you could call Server.Transfer( "~/file1.aspx" ) to forward the request to a particular file.
Or, you could transfer directly from your URL Rewrite rule and skip further processing in your Global.asax file. For instance, this rule will read the incoming URL that has no file extention, and then forward the request on to a file that has a file extension:
<rule name="Append a file extension to all requests discard querystring" stopProcessing="true">
<match url="^(.*)\?" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
Read more about URL Rewriting rules on Ruslan Yakushev's blog at http://ruslany.net/.
[EDIT] Okay, so for what you are asking...as far as I know, you do need to create a specific rewrite rule for each page. I was thinking more along the lines of MVC, where when you go to:
/news
it routes to
/default.aspx
which calls the NewsController.Index and displays the news page from default.aspx. However, to actually break everything out into individual pages and just trying to remove the extension...as far as I know, you have to create a new rule for each instance.
Redirect rules can be configured from the web.config files.
E.g. Here's what WordPress does in the web.config file that's included with WordPress:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule></rules>
</rewrite>
</system.webServer>
</configuration>
The routes ALL traffic to the index.php page. The index.php file then reads what the URL is and spits out data based on the URL. It doesn't redirect it to a different page after the redirect, it--rather--decides what content to display.
MVC works along the lines of, you see this url:
/news > will call > NewsController.Index();
/news/index > will call > NewsController.Index();
/news/view > will call > NewsController.View();
/news/read/id > will call > NewsController.Read(id);
These Controllers, then typically get data from the database and apply the data to a "View" (html page that sits somewhere on the server with variables to display the data the controller passes to it).
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've followed the instructions Learn IIS's webpage for adding static redirects with a rewrite map for my asp.net application.
The following is the config:
<rule name="Redirect rule1 for Information" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
And
<rewriteMaps>
<rewriteMap name="Information">
<add key="/Information/CorporateSales.aspx"
value="/KB/Information/CorporateSales" />
<add key="/Information/ComputerRepair.aspx"
value="/KB/Information/ComputerRepair" />
</rewriteMap>
</rewriteMaps>
This was even originally created by the wizard in IIS's manager for using rewrite maps.
So the idea is that /Information/CorporateSales.aspx --> /KB/Information/CorporateSales with a 301 redirect (MOVED PERMANENTLY).
However I'm just getting the original aspx page (Which we're removing later) loading. I've even deleted the file incase it was defaulting to an existing resource, and with that i just get a plain 404 without the redirect.
Anyone have an idea?
Let me clarify something:
Rewrite module works, it's installed and running. My standard regex rules work nicely. But my rewrite map does not.
This article http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module and code below worked for me.
<rewrite>
<rules>
<rule name="Redirect rule1 for RedirectURLs">
<match url=".*" />
<conditions>
<add input="{RedirectURLs:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="RedirectURLs">
<add key="/privacy.php" value="/privacy" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I was having a similar problem and found this question. It took me a little while, but I was able to figure out what the problem was.
My rewriteMap contained the urls "/Default2.aspx" and "/Dashboard.aspx".
When I would go to Default2.aspx, I would get a 404 rather than get redirected to Dashboard.aspx as expected.
The issue I found was that on my machine, the application was running in a subdirectory. The rewriteMap paths would only work if I used the full path (including the application folder), e.g., "/TestSite/Default2.aspx".
So I could have added duplicate entries in my rewriteMap to account for application directories on developer machines, but that seemed messy. I looked at the other rewrite rules in the application that did not have this issue and I noticed that they were using the {REQUEST_FILENAME} variable, rather than {REQUEST_URI}. So I switched the rule to use {REQUEST_FILENAME} and remove the first slash from the urls in my rewriteMap.
Do you have Url rewriting installed as part of IIS7/7.5? This is not installed by default. Also, make sure your app pool is set to integrated pipline mode, no classic.
Edit
From this:
http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/
This only thing I see that you're doing is adding the 'stopProcessing' attribute. Have you tried removing that?
Previously I had same problem as you described.
Could you update your code to
<match url="(.*)" />
and I hope you aware,
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
this condition will capture full URL except the domain.
example on this url:
www.example.com/Information/CorporateSales.aspx
it will check matching condition of
Information/CorporateSales.aspx on rewriteMap
and for sure it wont be match with this url
www.example.com/old/Information/CorporateSales.aspx
Did you reset the app pool and the iis site ?
In some cases it can take up to 72 hours (iirc) to propagate throughout the world.