How to redirect non-existent .html files to mvc3 routes? - seo

This is probably a duplicate, but my Google-foo is off or something, because I cannot find the answer to this simple question.
I have moved an old site to MVC3 on IIS7.5 and I need to redirect some old .html extension urls to new MVC Controller Actions.
I added URL mappings.
<urlMappings>
<!-- Doesn't work -->
<add url="~/OldUrl.html" mappedUrl="~/NewController"/>
<!-- Works but is rewrite instead of redirect -->
<add url="~/OldUrl.aspx" mappedUrl="~/NewController"/>
</urlMappings>
The above is not working for 2 reasons. First, the .html extension doesn't get re-mapped and second, the .aspx extension gets re-mapped rather than rewritten.
I tried to add the StaticFileHandler because I thought maybe the .HTML extension was being ignored by .NET but in fact that handler was already there.
I don't have access to IIS Management Tools because it is in shared hosting, but I should be able to just add something to the web.config to get these to redirect.

Finally figured it out:
<system.webServer>
<!-- The RewriteModule may need to be added if it isn't
already included in machine.config or application.config
<modules>
<add name="RewriteModule" />
</modules>
-->
<rewrite>
<rules>
<rule name="Redirect1">
<match url="^OldUrl.html$" />
<action type="Redirect"
url="/NewController"
redirectType="Permanent" />
</rule>
<rule name="Redirect2">
<match url="^OldUrl.aspx$" />
<action type="Redirect"
url="/NewController"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

Related

IIS Rewrite + Vue history mode making Windows Authentication not work

I'm creating a Vue webpage with history mode hosted on IIS. I use URL Rewrite to route any path that doesn't resolve to a static file to my index.html file, like many many forums suggested, and the Vue does client side routing from there.
However, I'm also using Windows Authentication, and any time I go to a Vue route (that doesn't match a static file) in my browser (tested on multiple), it asks for my credentials and I enter the correct ones, but it just keeps asking me again and again for my credentials, without ever letting me in. On the other hand, when I route to a static file, I log in, and it lets me in just fine and I can then go to a Vue route because I'm already logged in.
Why is IIS URL Rewrite making my authentication repeat itself and not let me in? We've been at a bit of a loss and have had to route to static file first every time to log in.
The only thing I've found is to turn off Anonymous Authentication, and it is.
Here is my web.config with the rerouting instructions:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Handle History Mode and custom 404/500" 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="/Dashboard/vue/dist/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
(It reroutes to /Dashboard/vue/dist/ because that contains an index.html)
Thanks!

IIS 8.5: Force all sites to use HTTPS

Wanting to know if there is a way to force all sites in IIS 8.5 to HTTPS instead of HTTP without having to create rewrite rules for each site we deploy to the box. We had one site get deployed to an internal server where the rules were not written in the config file and were just looking for a way to alleviate that miss in the future.
You can set this rule in applicationHost.config.
<rewrite>
<globalRules>
<rule name="http to https" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^Off$" />
</conditions>
<action type="Rewrite" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</globalRules>
</rewrite>

How to run vuejs files on IIS

So am using a vueJS Example as a demo for learning purposes which uses a requirejs-vue as a component loader.
when i run the server on Live-Server extension of VS-code (a simple http server), it run correctly with no problems.
However, when i run this example on IIS server (version 8.5) on windows 8.1, it through 404 Error app.vue does not exist.
Failed to load resource: the server responded with a status of 404 (Not Found)
and in the Network devtool, i can see that every module is loaded correctly, except for *.vue files,
VueJS Example :
https://plnkr.co/edit/Y2cEa3?preview
Web Config for IIS that am using:
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Handle History Mode and custom 404/500" 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>
PS: i have also run an other example that uses http-vue-loader instead of requirejs-vue and IIS still throwing the same error.
We need to set mimeType for the “.vue” extension file to use httpvueloader/requiresjs_vue.
In addition, we could manually add it to a Web.Config file, which can be recognized by IIS.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".vue" mimeType="application/javascript"/>
</staticContent>
</system.webServer>
</configuration>
Feel free to let me know if the problem still exists.

IIS Rewrite Reverse Proxy getting HTTP 502 error when using SSL

I am looking for ideas/suggestions to setup/troubleshoot a configuration that allows me to debug a scenario as follows.
I am using IIS 10, Visual Studio 2017
I have a web application that posts a message to a 3rd party site.
The 3rd party site then posts a response back to my web application.
IIS has a reverse proxy (shown below) the directs the post from the 3rd party application to IIS Express where
my app is running in IIS Express (VS 2017).
All connections use SSL. IIS Express is using a self signed cert that comes with VS2017.
Calling the app running on IIS Express from the 3rd party app via the reverse proxy worked until I changed to using SSL.
(This solution requires SSL).
url="https://10.10.203.132:44349/{R:1}" -> this is the computer where my app is running in VS2017.
Now I get a 502 error subcode 3 and my app is never called. No other info in Failed request tracing
Any suggestions?
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Reverse Proxy" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="https://10.10.203.132:44349/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="Ensure samesite Cookies" preCondition="Missing samesite cookie">
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; SameSite=None" />
</rule>
<preConditions>
<preCondition name="Missing samesite cookie">
<!-- Don't remove the first line here, it does do stuff! -->
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=None" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
<system.webServer>
<rewrite>
<rules>
<rule name="Reverse Proxy" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="https://10.10.203.132:44349/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT_SECURE}" pattern="0" /></conditions>
</rule>
</rules>
Here is another solution.
https://www.namecheap.com/support/knowledgebase/article.aspx/9953/38/iis-redirect-http-to-https
Feel free to let me know if the problem still exists.

Translating an Apache .htaccess file to an IIS web.config

I developed an application on my local using PHP, MySQL and Apache and it has a .htaccess file containing this:
#Setting the default handler.
DirectoryIndex home.do
<IfModule mod_mime.c>
#Supporting .do extensions
AddType application/x-httpd-php .do
</IfModule>
<IfModule mod_rewrite.c>
#Removing .do file extension if necessary
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.do -f
RewriteRule ^(.*)$ $1.do
</IfModule>
But I informed that my customer's web server is IIS and I have to use a web.config file instead of .htaccess. Can anyone direct me through this, please?
This could be seen as cheating, but we use ISAPI_Rewrite, which lets you just use the .htaccess file for IIS. If you can get them to put it on the server, you won't need to translate anything.
Please be aware that this will only work on IIS7 and not on IIS6. Also this requires FastCGI to be setup and the URL Rewriting module to be installed and enabled. These are things your hoster will be able to verify for you. If all of the above is true then the following file should do the trick ( you might need to tweak the paths but again I think your hoster will be able to do this for you if you supply them with this example file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<sectionGroup name="system.webServer">
<sectionGroup name="rewrite">
<section name="rewriteMaps" overrideModeDefault="Allow" />
<section name="rules" overrideModeDefault="Allow" />
</sectionGroup>
</sectionGroup>
</configSections>
<system.webServer>
<!-- Mapping the .do extension to the PHP ISAPI module -->
<handlers>
<!-- the following line is very specific to your host
please check the module name and the scriptProcessor
path with the system administrator! basically this is
the same as
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#EnableFastCGI
only in .config format. -->
<add name="MaskDoAsPHP" path=".do" verb="GET,HEAD,POST,DEBUG" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe" />
</handlers>
<!-- Setting the default handler. -->
<defaultDocument>
<files>
<clear />
<add value="home.do" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Removing do extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R1}.do" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
IIS7 and above can import Apache .htaccess rules using the URL Rewrite module.
Install the URL Rewrite module via the Microsoft Web Platform Installer
Start IIS Manager and on the left, in the Connections pane, select your required site (e.g. Default Web Site)
In the centre (Features View) double click URL Rewrite.
In the right panel click Import Rules... then paste your rules from the .htaccess file into the Rewrite rules box
Click apply in the right column.