Url Rewrite for angular urls - angularjs

I have Angular URL e.g "www.mycompany.com/employee/{78}" .
I want to rewrite this url as if i click on this employee and try to open it in new tab its using base url i.e. "www.mycompany.com/index.html/employee/78".
<rule name="main rule 2" stopProcessing="true">
<match url="^index.html/employee/[0-9]+$" />
<action type="Redirect" url="/employee/{78}" appendQueryString="true" />
</rule>
Here my question is how i can use the querystring ?? i.e. in this case employee id while rewriting the URL. Right now i have put the constant 78 but instaed of any constant i want to refer to "[0-9]+$" this value.

Found following answer. Need to put parenthesis for ([0-9]+) to use {R:1}.
<rule name="main rule 2" stopProcessing="true">
<match url="^index.htm/employee/([0-9]+)" />
<action type="Redirect" url="/employee/{R:1}" appendQueryString="false" />
</rule>

Related

Redirect to root page on 403 response

I have a React App hosted on IIS. If I access an existing directory, such as <app_url>/static/, it returns a 403 response since there's no permission on that directory. I'd like to redirect the user to my root page (~/).
I have tried this:
<httpErrors>
<remove statusCode="403" subStatusCode="-1" />
<error statusCode="403" prefixLanguageFilePath="" path="/" responseMode="ExecuteURL" />
</httpErrors>
This kinda works, it does return the root page, however, it tries to download the React bundles from /static/ instead of '/' only.
What can I do here?
I used this rule and it worked:
<rewrite>
<rules>
<rule name="Redirect Static" patternSyntax="ExactMatch" stopProcessing="true">
<match url="static" />
<action type="Redirect" url="/" redirectType="Found" />
</rule>
</rules>
</rewrite>
(Thanks to #samwu)

How to redirect all routes to index page in web config on azure?

I am working with a react application deployed on azure. I am using react routers Browserhistory module. Currently I have the following rule in my web.config on azure:
<rule name="admin rule" stopProcessing="true">
<match url="admin" />
<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" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
And this works, the /admin route can be refreshed and manually entered in the browsers url window. If this rule isn't applied I can't refresh or manually enter the url. (see issue: React-router urls don't work when refreshing or writting manually)
But I actually want this to work for every route, also dynamic routes. for example:
/cart
/product/uniqueid
/example
Wich rule can I write so that every route is being rewritten to the index like in the admin route?
You should be able to swap your admin match rule for <match url=".*" /> right above your opening <conditions> tag. Check this blog post for more information.

IIS Redirect all request on root

I have reactjs application with react router. I dont want to use hash history and I encountered the issue with refreshing page creating 404 error (react-router is not loaded when refreshing and browser wants to fetch non-existent content thus 404). I found some solution and I want to redirect every request to root so server always fetch index.html with import tags first.
My folder structure is simple:
ROOT/
-- index.html
-- static/
-- js/
-- main.js
-- css/
etc...
I have this web.config rule found on this site:
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
This works very fine with URLs on first folder path, I mean all request like /items, /contact are good. But requests like /items/detail are broken because browser is looking into items folder and not into document root. Also very important thing is that my webpack generate index.html script tags in this manner: <script type="text/javascript" src="./static/js/main.js">.
Is it possible that src attribute is wrong because of that ./? How can I tell server (IIS 10) "hey don't look anywhere else but into document root?"
Thanks guys.
This is a solution I found were I have some paths I still want to handle by my IIS but the rest I want react to take care.
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="^(.*)$" />
</rewriteMaps>
<rules>
<rule
name="redirect all requests"
stopProcessing="true"
>
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add
input="{REQUEST_URI}"
pattern="/someOtherResourceIstillWantIIStoHandle(.*)$"
negate="true"
/>
<add
input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true"
/>
<add
input="{REQUEST_URI}"
pattern="/resourcecentre(.*)$"
negate="true"
/>
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

ASP.net WebAPI ignoring web.config?

I am setting up a web.config in wwwroot to redirect all requests to index.html for angularjs to handle. This excludes existing files and folders and anything from /api.
The problem is, it's not working that way. I simply get blank pages for anything where a file does not exist.
Here is my web.config:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
<rewrite>
<rules>
<rule name="IndexHomeRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I can't figure out any reason for it to still be trying to load the files instead of redirecting them to index.html
I am using .NET Core RC2 and I have static files and default files set to be used. I am using VS2015. I set the project up as a WebAPI from the beginning. Not empty.
You can use Microsoft.AspNetCore.SpaServices:
app.UseMvc(options =>
{
options.MapRoute("Api",
template: "api/{controller}/{action?}/{*path}",
defaults: new { controller = "Home" });
options.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
});

cakePHP and IIS8.5 : HTTP Error 404.0 - Not Found

When I browse to localhost/admin/videos/view/1 I get the page I want. I also want that page to show when I browse to localhost/videos/view/1.
But it gives me error message :
HTTP Error 404.0 - Not Found
Detailed Error Information:
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Requested URL http://{my-ip}/app/webroot/videos/view/1
Physical Path C:\inetpub\wwwroot\{my-project}\app\webroot\videos\view\1
Logon Method Anonymous
Logon User Anonymous
These are my rules in the web.config file :
<rule name="Rewrite requests to test.php" stopProcessing="true">
<match url="^test.php(.*)$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/test.php{R:1}" />
</rule>
<rule name="Exclude direct access to app/webroot/*" stopProcessing="true">
<match url="^app/webroot/(.*)$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Rewrite routed access to assets(img, css, files, js, fonts, vid, favicon)" stopProcessing="true">
<match url="^(img|css|files|js|fonts|vid|favicon.ico)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
Update :
It has something to do with
<match url="^(img|css|files|js|fonts|vid|favicon.ico)(.*)$" />
When I cut the vid| (that is the directory of my videos) it shows localhost/videos/view/1.
But then I have the problem that it doesn't load my video.
What I've done to resolve this:
1 I deleted the |vid inside
<match url="^(img|css|files|js|fonts|favicon.ico)(.*)$" />
2 I replaced the vid folder inside my webroot to the files folder because that folder is standard there.
3 I changed the src to the video in my view
<source src="/files/vid/...

Resources