The point is, I can access an address dominio.com/modulo/id/titulo and it rewrites to dominio.com/default.asp?link=artigo&id=123&titulo=teste, but my question is whether I can do the reverse process, i.e. go to dominio.com/default.asp?link=artigo&id=123&titulo=teste and it changes to dominio.com/modulo/id/titulo.
Codes:
ASP
<!DOCTYPE html><html lang="pt-br"><head><meta charset="utf-8"/><title>Teste Isapi Rewrite</title></head><body><p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>teste: <%=request("teste")%><br></p></body></html>
WEB.CONFIG
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="artigo" stopProcessing="true">
<match url="^artigo/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?link={R:0}&id={R:1}&teste={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You could use below url rewrite rule:
<rule name="reverse" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="default.asp" />
<add input="{QUERY_STRING}" pattern="link=(.*)\&id=(.*)\&titulo=(.*)" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" enabled="true" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?link={R:1}&id={R:2}&titulo={R:3}" appendQueryString="false" />
</rule>
default page code:
<!DOCTYPE html><html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Teste Isapi Rewrite</title>
</head>
<body>
<p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>titulo: <%=request("titulo")%><br></p>
</body>
</html>
What I think you're after is a rule which redirects the "unfriendly" URL to the "friendly" one - ie if you paste the unfriendly one into your browser address bar then it changes to the friendly one when your page appears in the screen. Janvi Panchal has the right idea above. What you do is have a redirect rule and a rewrite rule as follows.
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^dominio\.com/default\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^link=([^=&]+)&id=([^=&]+)&titulo=([^=&]+)$" />
</conditions>
<action type="Redirect" url="dominio.com/default/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^dominio\.com/default/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="dominio.com/default.asp?link={R:1}&id={R:2}&titulo={R:3}" />
</rule>
Note that the redirect rule comes first, so what's happening is that the location is redirected to the friendly URL which is then rewritten as the unfriendly one.
Also note that I didn't hand code this - I generated it with the URL rewrite wizard in IIS Manager. Click on the URL Rewrite icon, then Add Rule then User Friendly URL. Check the "Create corresponding redirect rule" before you click the OK button and this code will be generated and appended to your web.config file in the appropriate location
Related
I have the following rules, but when I access https://myserver/myapp/sparoute/1 I get a 403 instead of the index.html
I got this config at root of myapp:
<system.webServer>
<rewrite>
<rules>
<rule name="Reverse Proxy to API" stopProcessing="true">
<match url="api/(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="https://internalserver/myapp/{R:0}" />
</rule>
<rule name="Reverse Proxy to Signalr" stopProcessing="true">
<match url="messagehub(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="https://internalserver/myapp/{R:0}" />
</rule>
<rule name="Handle History Mode for SPA (myapp)" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The first two rules are working, but instead of returning the default index.html when rewriting to / i get a 403. Any ideas?
The rewrite url is relative to website. so the last action should be
<action type="Rewrite" url="/myapp" />
I want to create a rule using IIS URL-Rewrite module. I want to redirect all pages from www.mydomain.com to mydomain.com
However, there are some pages on teh site that I do not like to have redirection. Those pages are
www.mydomain.com/mail/default.asp
www.mydomain.com/mail2/default.aspx
So here is my code so far
<rule name="Force non-WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{REQUEST_URI}" pattern="^/mail/(.*)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/mail2/(.*)" negate="true" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" />
</rule>
However, if I enable this rule and I go to www.mydomain.com/page, I got 500 error.
What is wrong with my code?
Just add condition <add input="{REQUEST_URI}" pattern="/some-url/(.*)" negate="true" />.You could use below rule:
<rule name="Force non-WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="/mail/(.*)" negate="true" />
<add input="{HTTP_HOST}" pattern="(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:2}/{R:1}" appendQueryString="true" />
</rule>
Regards,
Jalpa.
I have a AngularJS application that utilizes URL Rewriting for linking. My rewrite rule looks like :
<rewrite>
<rules>
<rule name="MainRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="Pattern" pattern="^api/(.*)" negate="false" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
I found this solution on SO: How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? and made one modification to allow my API to pass through.
Essentially, I want to redirect all my requests to Default.cshtml EXCEPT calls to my Web API. This works GREAT when I'm I load the app on the base URL like: localhost/myapp. However, if I reload the page on a angular route like: localhost/myapp/dashboard it fails saying:
<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost/myapp/dashboard'.</Message>
<MessageDetail>No type was found that matches the controller named 'dashboard'.</MessageDetail></Error>
I have a work around where I did:
<rewrite>
<rules>
<rule name="Default" stopProcessing="true">
<match url="^(?!lib|api|dist|assets|app/|bower|common|main|signalr|templates|bower_components/).*" />
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
and it worked fine. Any ideas how I can utilize a cleaner solution above and still accomplish the rewrite?
I need to change the pattern and change {REQUEST_FILE} to {REQUEST_URI} in a couple of places. See my demo here:
<rewrite>
<rules>
<rule name="MainRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
I've got a rule that rewrites URLs unless a physical file exists (so static files can be returned) - however for some reason the rules get rewritten anyway, to much frustration.
Here's my Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Front Page">
<match url="/?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="/Home/FrontPage" />
</rule>
<rule name="Map Everything" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="EntryPoint.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
Ah, it turns out the "Front Page" rule was the problem, and the rule "/?" was matching everything when I should have used "^$" instead.
After fixing that, the IsFile/IsDirectory rules were being respected.
I Have some problems with redirecting to another URL based on the query string parameters.
I have url for mapping service, for example "http://www.mydomain.com/?lon=111&lat=222&zoom=3"
and I need it to be redirected to "http://www.mydomain.com/111/222/3".
I have this rule in my web.config, but it does not work
<rule name="Location redirect">
<match url="\?lon=(\d+)&lat=(\d+)&zoom=(\d+)" />
<action type="Redirect" url="{R:1}/{R:2}/{R:3}" redirectType="Found" />
</rule>
The reason is that URL does not include Query String, you ned to use conditions for that. If you use the User Interface, it includes a friendly URL template that will generate it for you.
Below you will find the Rewrite rule as well as the redirect so that legacy traffic (ugly URL) is redirected to the pretty URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^lon=([^=&]+)&lat=([^=&]+)&zoom=([^=&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="?lon={R:1}&lat={R:2}&zoom={R:3}" />
</rule>