Url Rewrite from mysite.com/home/ to mysite.com/ in asp.net core - url

My rewrite rule in web.config properly redirects from "example.com/home" to "example.com/", but fails to redirect from "example.com/home/" (the only difference being the forward slash in the latter). I'd like to know how to get the second example to work. My web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="mysite.com/home or mysite.com/home/ to mysite.com/" stopProcessing="true">
<match url="(.*)\/?\bhome\/?\b$" />
<action type="Redirect" url="/" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>

Related

URL encode/decode issue - Jenkins and reverse proxy with IIS(rewrite)

I have using reverse proxy(via ARR). I have following rule setup in web.config.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Reverse proxy for jenkins" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(http?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://localhost:8080/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I get following error while browsing this url. http:// jenkins.mytest.com/user/dmn%5Cmda
HTTP ERROR 404 Not Found
URI: /user/dmn/mda
STATUS: 404
MESSAGE: Not Found
SERVLET: Stapler
dmn-domain
mda-username
whereAs without domain(and slash) in URL it works well. As following
i.e http:// jenkins.mytest.com/user/mda
I have the following configuration for IIS precisely and it has worked for me, let me know if it is useful to you.
<configuration>
<system.webServer>
<rewrite>
<rules useOriginalURLEncoding="false">
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="{C:1}://mydomain.com:8088{UNENCODED_URL}" appendQueryString="false" />
<conditions>
<add input="{CACHE_URL}" pattern="^(http|ws)://" />
</conditions>
<serverVariables>
<set name="HTTP_FORWARDED" value="for={REMOTE_ADDR};by={LOCAL_ADDR};host="{HTTP_HOST}";proto="http"" />
</serverVariables>
</rule>
</rules>
</rewrite>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>
Additionally as a guide: https://www.jenkins.io/doc/book/system-administration/reverse-proxy-configuration-iis/

WWWROOT in MVC5

How to achieve same behavior in ASP.NET MVC5 with static files like it works on aspnet-core with app.UseDefaultFiles(); app.UseStaticFiles();?
I mean serving static files from some folder over root, e.g. /wwwroot/some.html must be opened on mysite.com/some.html, /wwwroot/img/test.jpg on mysite.com/img/test.jpg etc.
Update: I have created wwwroot folder and added following rule to web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Static" stopProcessing="true">
<match url="^(?!(wwwroot/|api/))(.*)$" ignoreCase="true"></match>
<action type="Rewrite" url="/wwwroot/{R:1}" />
</rule>
</rules>
So IIS must return files from wwwroot except when calls go to /api/something, but I'm always getting index.html in wwwroot folder and never other files. Api's URL works good.
What I'm doing wrong?
All works in that way:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Static" stopProcessing="true">
<match url="^((?!(wwwroot\/|api\/))(.*))$" ignoreCase="true"></match>
<action type="Rewrite" url="/wwwroot/{R:1}" />
</rule>
</rules>
</rewrite>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="StaticFile"/>
<add
name="StaticFile"
path="*" verb="*"
modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
resourceType="Either"
requireAccess="Read" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
<staticContent>
<mimeMap fileExtension=".*" mimeType="application/octet-stream" />
</staticContent>
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
Don't forget to install rewrite module.

Remove section in web.config file in debug mode

I have an asp.net application, which must run under SSL, and it has some rewrite rules defined in web.config to accomplish this.
<!--file web.config -->
....
</system.webServer>
<rewrite>
<rules configSource="webrewrite.config" />
</rewrite>
</system.webServer>
<!--file web.config -->
<rules>
....
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
....
</rules>
However, in development mode (with local web server or IIS Express) I don't want to use SSL.
So I would like to be able to use web.config transformations to remove one or more rewrite rules (but not all)
If you want to remove the Entire Section for your Dev Configuration use
<system.webServer>
<rewrite xdt:Transform="Remove" >
</rewrite>
</system.webServer>
I solved the problem, by using Remove transform, as shown below
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
....
<system.webServer>
<rewrite>
<rules>
<rule name="RulaNameToRemove"
xdt:Transform="Remove"
xdt:Locator="Match(name)" >
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please write below code web.Debug and web.Release config file. Web.Debug will delete rewrite rules and web.Release insert rewrite rules.
Web.Debug.config
<system.webServer>
<rewrite>
<rules>
<rule name="RulaNameToRemove"
xdt:Transform="Remove"
xdt:Locator="Match(name)" >
</rule>
</rewrite>
</system.webServer>
</configuration>
Web.Release.config
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true" xdt:Transform="Insert">
<match url=".*"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
You can move release settings to web.config.release file

Add Child Section to Web.config.release

In Web.config I have the system.webServer section:
<system.webServer>
<!-- Config Child Sections -->
</system.webServer>
In Web.config.release I need to add a new child section to system.webServer so I used:
<system.webServer>
<rewrite>
<rules>
<rule name="Enforce WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
When I build it in release the rewrite section is not added.
How can I do this?
Thank You,
Miguel
You need to add xdt:Transfor="Replace" to your tag:
<system.webServer xdt:Transform="Replace">
Without this the transformer don't know what to do with the tag. For more info please see: http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/web-config-transformations
****EDIT********
Then you could try:
<rewrite xdt:Transform="Insert">

Deployment silex: white page

I want to put my first silex project online on a webhost.
Currently the only thing I can see is a white page, and it takes a while before my browser actually finds the page.
My webhost: www.mijnhostingpartner.nl
My current structure:
/root/data
/root/logs
/root/wwwroot
/root/wwwroot/app
/root/wwwroot/src
/root/wwwroot/vendor
/root/wwwroot/web
My web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<rewrite>
<rules>
<rule name="Silex Front Controller" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<clear />
<add value="Default.html" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="Default.aspx" />
<add value="index.html" />
<add value="index.php" />
<add value="index.asp" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
My index.php:
I found out that the "Require_once" did not work.
Only until echo('test1') works.
"test2" is not shown on the screen.
<?php
// PHP 5.4's built in server can now server static files
// #see http://silex.sensiolabs.org/doc/web_servers.html#php-5-4
$filename = __DIR__ . preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;
}
echo('test1');
// Require the app and run it
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'app.php';
echo('test2');
$app->run();
If you get a blank page, check your webserver's error logs. Then enable error_reporting in php.ini if possible. And for silex, make sure you are calling $app->run() in the front controller.
(the latter does not seem to be the issue in your case)

Resources