Webconfig URL Rewrite to Hide Classic ASP ext but not .aspx or other ext - asp.net

I have been able to hide the .asp extension, but it also removes any other extensions and points to .asp
I know this is just a config issue but have no experience of the webconfig file configuration and wonder if anyone has a quick solution to save me a few hrs!
Code as I have it below from part of webconfig file
<rewrite>
<rules>
<rule name="Hide .asp Ext">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.asp" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.asp" logRewrittenUrl="true" />
</rule>
<rule name="Redirect .asp Ext" stopProcessing="true">
<match url="^(.*).asp" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).asp" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>

Seems to me the first rule converts everything to an .asp extension first?
Try removing the first rule.

Try this? Seems to work on my test server.
<rule name ="redirect .asp to none" stopProcessing="true">
<match url="(.*)\.asp$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .asp extension" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" />
</rule>

You have two rules defined. It looks like the first matches on all requests and "rewrites" them to have a .asp extension. The second matches on on .asp and then performs a redirect of some kind.
What is not clear from your question is how you want to 'hide' these files. If you want to deny all requests to .asp you would be much better served adding a Request Filtering "File Extensions" https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/index

Related

Hide .php extension in web.config (windows server) running wordpress

Before anyone makes this a duplicate question please hear me out. I have researched several ways to do this that should work but I think because wordpress is running on this server it's causing conflict.
The setup: wordpress site running on a windows server. However, I have files in the webroot outside of the wp-content folder that users need to access. I need the extension removed on theses files.
When I use this:
<rewrite>
<rules>
<rule name="Redirect .php extension" stopProcessing="false">
<match url="^(.*).php$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).php$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .php extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
</rules>
</rewrite>
It takes the extesion off the urls but the pages outside of wordpress do not work. I think because of this which is in the web.config already:
<rule name="WordPress: http://hiddenwebsitewp.azurewebsites.net" 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>
Anyone have this problem before?

URL Rewrite in IIS 7 causes redirect loop

I have tried various methods found on the web (including some SO answers) for getting URL rewrite in IIS 7 to work so that I can turn mysite.com/somepage.aspx into mysite.com/somepage, for example. The last thing I've tried is the video at this link: https://www.youtube.com/watch?v=bBNJE7XA1m0. After applying these changes in IIS, I now can request mysite.com/somepage and get to mysite.com/somepage.aspx with the .aspx removed in the address bar. Partial success.
When I try to directly request mysite.com/somepage.aspx, however, I get into a redirect loop. I am hoping there is some simple mistake in my settings. Here is the web.config section created by making changes in IIS:
<rewrite>
<rules>
<rule name="HideAspxExtension">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>
<rule name="RedirectingAspxExtension" stopProcessing="true">
<match url="^(.*).aspx$" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="^(.*).aspx$" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
I have tried to apply this setting to multiple applications and I get the same results. What I do not have is another server to test on.
These are rules I've used before:
<rule name="StripAspx">
<match url="^(.+)\.aspx$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Rewrite the .aspx for internal processing-->
<rule name="RewriteASPX" 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="{R:1}.aspx" />
</rule>
The main differences I can see are:
These have the removal of the .aspx first (in rewrite rules, order matters). This also means that in these rules the stopProcessing directive is on the rewrite to .aspx, not the redirect away from it.
These don't have <add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />

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>

URL Rewriting - removing the entire page name

Currently I have a rewrite rule in my web.config that removes the ".aspx" page extension.
I want to rewrite my URL to remove the whole page name "page1.aspx" so my URL will show as "www.mysite.com/folder/" and not "www.mysite.com/folder/page1"
My rewrite code in the web.config I currently use is the following :
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([._0-9a-z-/]+).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>
What do I need to add or edit to my current rules to get the results I need?
Thanks!
Quickly testing it on regex101.com, does this work?
<match url="^([._0-9a-z-/]+)/(.+).aspx$" ignoreCase="true" />
and
<action type="Redirect" url="{R:0}/" />
Adding the "/(.+)" makes sure the last word before aspx gets removed from the first match (R:0). R:1 will contain that word: "page1" while R:0 will contain:"www.mysite.com/folder".

redirect issue on webconfig file

please check sfveinaesthetics.com/index.php opens but http://sfveinaesthetics.com/ dont.. its a wordpress site in iis7 server.
inner pages work http://sfveinaesthetics.com/about-us/ fine..
Please get a me solution asap.. web config is like
<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/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Not sure if it's causing the problem but the default rewrite rule for Wordpress does not include the /{R:0} part in the rewrite action. Normally it's:
<action type="Rewrite" url="index.php" />

Resources