Linux to windows migration, converting htaccess to web.config - web-config

I am trying to setup a php application on a windows server for a client. But my experience with windows is minimal and I am currently trying to setup the web.config file.
Below is my current htaccess on the linux server. How do I setup the web.config file and do I place this on the website root folder as as htaccess?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

So after research I found some answers and below is the file that worked for me on a zend framework application. Hope this will help someone trying to setup a zend app on a windows server.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Ignore files on the filesystem" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

My IIS redirect rule is not working via web.config

I have a redirect rule that I am trying to create using web.config. I am trying to redirect
from the below page:
https://example.org/content/content.php?PrintMe=0&PgName=gothedistance
to this page:
https://example.org/gothedistance
My url redirect rule looks like the below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="content.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="PrintMe=0&PgName=gothedistance" />
</conditions>
<action type="Redirect" url="gothedistance" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

How do I redirect a specific port in the IIS server to an other port

My URL Rewrite rule only works for port 80 in IIS .
The rewrite works for : http://localhost:80 --> http://localhost:8100
The rewrite works for : http://localhost:80/redirect --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080 --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080/redirect --> http://localhost:8100
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT}" pattern="^8100$" negate="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}:8100/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The web.config is only working on default http port 80/443 but not for an specific port.
Which changes has to be made in the web.config for working with all ports?
The main problem is the variable {HTTP_HOST} which already contains the PORT information.
To solve this issue you need to adjust two things:
Extend the matching pattern within
Set the correct address for the redirect
I adjusted your web.config to see how it should work with your example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" >
<add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
</conditions>
<action type="Redirect" url="http://{C:1}:8100/{R:0}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Redirect all pages to index

I have a simple website with a few pages, I just want all the other requests to be redirected to index page instead of showing 404 page.
My host is a plesk and I've built site with
That is how WordPress works - all requests to non-existent files or directories are redirected to index.php, processing second part of URL as an argument for index.php script.
On Plesk for Linux, You can add required directives to the additional section in Domains > example.com > Apache and Nginx Settings. On Windows - to web.config file in the document root.
Apache mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Nginx rewrite:
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php last;
}
IIS URL rewrite:
<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>

ASP.NET httpRedirect exactDestination for all pages except root

I'm changing the primary domain for an IIS website, and I need to keep existing URLs working. At the same time I am changing the behaviour of the start page of this (the previous) domain. This domain has previously been accessible at its root level, but now I need the root page (default document) to redirect to a specific subdirectory.
In summary: I need to, while redirecting, keep the trailing path for all pages except for the default document (root). Example:
www.old.com/ -> www.new.com/en
www.old.com/en/page2.htm -> www.new.com/en/page2.htm
www.old.com/en/page3.htm -> www.new.com/en/page3.htm
www.old.com/page1.htm -> www.new.com/page1.htm
I have this web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com" httpResponseStatus="Permanent" exactDestination="false" />
</system.webServer>
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com/en" httpResponseStatus="Permanent" exactDestination="true" />
</system.webServer>
</location>
</configuration>
I have set the IIS setting Default Document to Default.aspx.
But this doesn't work.
I solved it by using the URL Rewrite Module in IIS instead:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root request" enabled="true" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://www.new.com/en" redirectType="Found" />
</rule>
<rule name="Path request" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<action type="Redirect" url="http://www.new.com/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS 7.5 HTTP to HTTPS redirect

I am trying to have all HTTP requests redirect to HTTPS.
Here is my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Execute, Script" />
</system.webServer>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I found this sample in another post on Stackoverflow:
How to force HTTPS using a web.config file
When I save that file and try to access my site using http it does not redirect to https.
I thought that maybe the file was being ignored so I typed some incorrect syntax into the web.config then I got a 500 error- meaning that it is indeed looking at the web.config file.
Am I misunderstanding what the above configuration is supposed to do? I want to redirect all HTTP request to HTTPS.
The site that is being redirected to is a virtual directory for Tomcat if it makes a difference.
Assuming you have URL Rewrite Installed. Click here for info/installation.
Make sure you have the following configured within the URL Redirect in the IIS Manager.
Match URL Section
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*)
Make sure Ignore Case is checked
Conditions Section
Logical Grouping: Match All
Input: {HTTPS}
Type: Matches the Pattern
Pattern: ^OFF$
Action Section
Action type: Redirect
Action Properties
Redirect URL: https://{HTTP_HOST}/{R:1}
Make Sure 'Append Query String' is checked
Redirect Type: See other (303)
Just Try this:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>