Extensionless URLs but keep Extension in the admin area - asp.net

I am using ASPDOTNETSTOREFRONT and we need to remove the extensions for the site but keep the extensions in the /admin folder so the back end will work correctly. Below is what I have so far.
<rules>
<rule name="Extensionless" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" negate="true" pattern="\.axd$" />
<add input="{URL}" negate="true" pattern="\.aspx$" />
<add input="{URL}" negate="true" pattern="\.ashx$" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
<rule name="RemoveExtension" enabled="true" stopProcessing="true">
<match url="(.*)\.(aspx)$" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>

Related

Rewriting URL causing opening folder with same name

I have the following rule to remove .aspx from the end of url:
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="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="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Now the problem is that a few specific pages with the same name of folder available in that path, gets opened every time instead of that site.
This is my site
Suppose when i click a link City.aspx, it opens the folder(in FTP) with the name City instead of opening that page.
[Edited]
Removing <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> solved the problem but now entering the sitename in the url is not working.
Solved the problem by removing this:
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
and adding this:
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Rewrite" url="http://www.{C:0}/{R:0}/Index.aspx" redirectType="Permanent"/>
</rule>

ASP.net OWIN AngularJS html5mode url redirecting

I am trying to setup a single page application with a Web API restful back-end. I used the "OWIN WebAPI SPA Template" to start my project. This template defaults the static file serving to the public/ folder within the solution's root directory. I want to support html5 urls, IE. localhost/madeUpPath which should hit the index.html page. In order to do this, I setup a rewrite rule in the Web.config file:
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="api/" ignoreCase="true" negate="true" />
<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>
The issue I am having is that since the static files live in /public/ the {REQUEST_FILENAME} thinks the file is relative to / and the url localhost/main.css is rewritten to localhost/
I have tried changing the rule to this:
<add input="public/{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="public/{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
but that did not work. How can I achieve my desired result? Thank you.
Edit: I found something that seems to work, but isn't exactly what I was looking for.
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="([a-zA-Z0-9-_.\/]+\.(css|js|less|json|jpeg|jpg|png|gif|svg|xml|html))$" />
<action type="Rewrite" url="/public/{R:0}" />
</rule>
<rule name="Second Rule" 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>
Firstly, this code doesn't because you're using OWIN server for static files:
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
Could you please to use next code:
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="^((?!(api|\.)).)*$" />
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
Regular expression gives us ability to ignore api and any file paths.
We also use Owin to serve content out of a sub folder and the following works perfectly for us:
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsDirectory" ignoreCase="true" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
It's very similar to this comment, but I changed their {REQUEST_URI} variables to {URL} so that it will also ignore existing files even if they have query strings, which is necessary for cache busting resources, like site.css?ec99043d9af49f2bd7c2.
Edit: I also just found this Microsoft example which with some tweaking may also work for this scenario, but I haven't tested it myself:
<rewrite>
<rules>
<rule name="Angular Routes" 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="/MyApp/" />
<!--<action type="Rewrite" url="/" />-->
</rule>
</rules>
</rewrite>

How would one rewrite the url to remove the aspx extension and also redirect a trailing slash?

I want to remove the aspx extension for clean urls, and also redirect when a trailing slash is used.
In my web.config file:
<!-- Rewrite, adding extension -->
<rule name="add aspx">
<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}" negate="true" pattern="\.axd$" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
<!-- Removes trailing slash after rewrite -->
<rule name="remove trailing slash after rewrite" stopProcessing="true">
<match url="(.*)/.aspx$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
So, basically if one requests the url "mysite/mypage", this will have the aspx extension added on and work okay, and when one requests "mysite/mypage/", they get redirected to "mysite/mypage".
My solution seems to work, but is there a more efficient way to do it with just a rewrite and not a redirect?
I didn't realize that order matters, so I did the following: first redirect the trailing slash, then do the rewrite.
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="Add aspx">
<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}" negate="true" pattern="\.axd$" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
This seems to be the best way, unless someone knows a better one.

Wordpress - IIS 7.5 - URL rewrite - OWA and RWW 403.18 Forbidden

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" 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="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This rule is located in my wwwroot folder, it fixes my URL's, but it breaks my OWA and RWW websites.
How can I add an exception, or condition, to this rule so that it ignores a particular address? I am trying to process the domain name remote.mydomain.com, normally.
With the rule above in place, remote.mydomain.com, will return a 403 error.
You can simply add a condition to your rule:
<rule name="Main Rule" 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="{HTTP_HOST}" pattern="^remote\.mydomain\.com$" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
the condition
<add input="{HTTP_HOST}" pattern="^remote\.mydomain\.com$" negate="true" />`
will prevent your rule from trigger if the requested host is exactly remote.mydomain.com

Url rewrite on both www and non-www

I have a couple of rules in IIS to redirect incoming requests to pages with file extensions, so for example mysite.com/hello/ is directed to www.mysite.com/hello.aspx
I have a rule which caters for the trailing slash so I don't end up with www.mysite.com/hello/.aspx
this is fine but it's not working for requests that start with www
so, www.mysite.com/hello is staying the same
I tried putting an additional rule for anything matching the pattern www* but doesn't seem to work.
Any advice much appreciated
<rule name="Remove trailing slash" stopProcessing="false">
<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="{R:1}" />
</rule>
<rule name="AddASPX" enabled="true">
<match url=".*" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>

Resources