web.config & Aruba - URL Rewrite - iis

I'm developing my first website and just learnt about the web.config file for IIS, so what I'm trying to do is to hide different parts of the url.
For example the homepage is www.domain.com/it/homepage.html and I'd love to make it appear as www.domain.com/it/homepage
I haven't found anything about removing an .html extension on the net, just a lot of .aspx which I tryed to replace with .html, failing miserably.
Another side question: is it possible to redirect errors with web.config ? I've seen a lot of .htaccess based solutions but that's it.
EDIT: Forgot to say that the website is hosted by ARUBA, on their windows oriented server

You are trying to change the URL of your site, which is the way to access it... that's a pretty odd action to do. When you get a request to your server for a certain page, the URL is parsed as the path to that file. If you want to change the URL, you need to change the name of the file - and if you wouldn't like having a .html in the end, then perhaps the file shouldn't be a html file - however, your goal is different. You want to have no extension at all.
Luckily, there is a way to have a url endpoint which doesn't have any file extension is all. That would be a folder. Folders dont have an extension to them, however they have no content by themselvs except the file they're linking.
Side note: There's another way, which is the one you'd usually see around the web. PHP could have other things in the URL than just the endpoint, but you asked about windows based HTML/ASPX windows backed pages.
So - yeah, the only way I see to have no extension at all is letting the user navigate through folders. The only question left is, why would you want this anyway?

You can use the following configuration in your web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="niceURL" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

IIS URL Rewriting - Change a querystring key and value into a route

I've recently implemented IIS URL Rewriting on one of my websites. I'm using URL Redirection rather than rewriting for everything. All of my static redirections are working perfectly, however, there's one specific type of dynamic redirection that I can't seem to make work.
I had old URL's that looked like this:
http://example.com/?tag=mytag
I'd like this to be redirected to the new URL format of:
http://example.com/tag/mytag
For these URL's the querystring key (tag) is known and fixed, however, the querystring value ("mytag" in the example above) is entirely dynamic and unknown in advance (so I don't believe it's therefore possible to use IIS Rewrite Maps).
Is is possible to add an IIS Rewrite rule that performs this kind of redirection for all possible querystring values that may be supplied?
Yes, the guts of the solution are below. Whats going on is...
1st condition means only apply this rule to the top level of the site. So http://example.com/?tag=mytag will redirect, whereas http://example.com/foobar/?tag=mytag wouldnt.
2nd condition is the magic. It only runs if a query param called tag exists, and the (.*) is a regex to grab the value for use in the new URL.
The action uses the value you grabbed in the 2nd condition referenced as {C:1}. appendQueryString does exactly what it says - set as appropriate. redirectType should be left as Temporary (HTTP response code 307) until your happy, then change it to Permanent (HTTP response code 301). Once you send a 301 response the client(/search engine) will potentially cache the response and not re-request from the server causing problems if you make a mistake.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect based on tag query value">
<conditions>
<add input="{REQUEST_URI}" pattern="$" />
<add input="{QUERY_STRING}" pattern="tag=(.*)" />
</conditions>
<action type="Redirect" url="tag/{C:1}/" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Coldfusion - Avoid IIS 404.0 error for specific files and do manual routing instead

I am using coldfusion 10 and iis 7.5 and trying to create a custom routing for my project that avoids using query string.
For example, the following URL:
www.myTestWebsite.com/subfolder1/subfolder2/
should be passing from a router.cfm file that exists in the root folder and then based on the string after the website's domain (in this case /subfolder1/subfolder2/) coldfusion will decide what to show and how.
I managed to make everything work except for one thing. If index.cfm (or .html, or any default file) does not exist in the /subfolder1/subfolder2/ path, IIS shows a 404.0 error. However, if create the folder structure and add an empty index.cfm file there, the routing works as expected, ignoring that empty index.cfm and moving on according to the logic I have implemented.
Is there a way (through CF Administrator, IIS, htaccess or any other) to avoid checking if that file exists and throwing a 404.0 and instead allowing me to handle that through my route.cfm?
I use rewriting in web.config:
<rule name="wvcms" patternSyntax="Wildcard" 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="/route.cfm?cmspath=/{R:1}" />
</rule>
You can do this using a custom ColdFusion servlet mapping. The information below comes from the Taffy ColdFusion REST API framework documentation.
http://docs.taffy.io/3.1.0#404-when-your-api-is-in-a-subdirectory
In your web.xml, you need to add an additional servlet mapping:
<servlet-mapping>
<servlet-name>CFMLServlet</servlet-name>
<url-pattern>/api/index.cfm/*</url-pattern>
</servlet-mapping>
This is because Tomcat doesn't support the use of two wildcards in its mappings. You'll notice that installing ACF or Lucee in Tomcat you'll get a web.xml with mappings that have a url-pattern of index.cfm/*, but unfortunately because of this limitation, you can't change that to /index.cfm/.
In the xml above you can see that I only have 1 wildcard, but to compensate I've specified the entire path to index.cfm, so that only 1 is needed. (Note that I used /api/index.cfm because it matched my example of a 404 for /api/index.cfm/myResource... yours should match the location of your index.cfm).

web.config 301s for all files but one

I'm trying to set up 301s for a site which will bounce all old pages to the homepage (it was a large site that is now a single page).
The server's Windows based, however, so no htaccess fun for me.
Could anybody assist with producing a web.config file that redirects everything to the homepage via 301s?
This may work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301 Redirect Everything To Home" stopProcessing="true">
<match url=".+" />
<action type="Redirect" url="/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But is will redirect everything, including images, include files etc.
Could you explain your motive for this as generally it's a bad move. It causes a poor user experience, robots don't like it, and nothing knows if a page is really missing or not.
I suggest you to try "IsapiRewrite" 3rd party module/DLL
ISAPI_Rewrite is a powerful regular-expressions-based URL rewriter for
IIS. It is compatible with Apache mod_rewrite making it possible to
move configurations from Apache to IIS and vice versa just by copying
.htaccess files (please see this compatibility chart). It is used for
search engine optimization, to proxy another server's content, stop
hotlinking or strengthen server security.

IIS - Mapping one extension to another

I did a bit of search but can't find a complete answer to my question:
I wonder if it's possible to set my IIS 7.5 to map one "custom" extension to another "physical" extension.
Eg.
I have Default.aspx, I'd like my server to serve this file to a request of Default.foo.
The mapping questions/answers I found are all about having Default.foo files, and mapping them to the proper handler; that's not my case, I just like to have a sort of masking of the real physical file extension.
Is it possible?
TY
The mapping should be setup between default.foo and default.aspx, mapping extensions cannot achieve this goal. You may use URL Rewrite module to create a rule to rewrite default.foo to default.aspx.
A simple example is as below,
<rewrite>
<rules>
<rule name="foo" stopProcessing="true">
<match url="(.*)\.foo" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>

How can I indicate to users that my IIS website is undergoing maintenance?

For my IIS website, I'd like to redirect ALL requests to ONE page. The purpose of this is that I want to do some maintenance on the database (take it off-line) that all my web applications use. I have about 50 web apps running under this website, so I'd like to avoid visiting each of them to change something. I'm thinking I could make a single change in machine.config? Any hints would be appreciated.
If you are using ASP.NET 2.0 (or higher), you can drop an app_offline.htm page on the root.
More info here.
in webconfig
<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.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Make all the pages un-available, probably stop the current web site and create an entire new completly blank site in its place. Then put up a custom error page for the 404 (file ot found) error. Custom Errors is a tab on the properties dialog of the web site in IIS. Just create the page you want to send, then change the entry for 404 on the custom errors tab to point to the new file you just created.
In IIS 10 there is an optional component "HTTP Redirect" (it may be available in earlier IIS versions; I don't know).
It allows you to set up very simple catch-all redirects, using any of the common HTTP redirect response codes.
This can be installed via Server Manager, in Windows Server 2019.
Could you create a new site in IIS with a binding to port 80 with a blank host-header (much like the Default site) and then stop the other site(s)? That way all requests would be handled by the new site, which could simply be a static HTML page notifying users that the site is down for maintenance.

Resources