Apache .htaccess rewrite rule to IIS web.config rule - .htaccess

I am moving a PHP application from Apache to IIS server. I am trying to translate the following .htaccess rule:
RewriteEngine On
RewriteRule ^(.*)$ public/$1?%{QUERY_STRING} [QSA,L]
It simply redirects all requests (as well as query string) to public subdirectory.
I have tried something like the following in web.config file-
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/public/index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But it keeps falling into redirect loop. What should be correct web.config rule in this case?

Managed to solve this by importing rules in IIS.
http://akrabat.com/winphp-challenge/zend-framework-url-rewriting-in-iis7/

Related

HTTPS redirect in .htaccess is causing infinite redirect loop

I'm trying to redirect all HTTP urls to HTTPS using the following in .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However it just causes an infinite redirect loop. There is a valid SSL certificate and the website is written in asp.net in case it matters. Google chrome automatically redirects to https, but firefox and edge do not so I need to add this rule. The only way I can get the site to load in https is if I explicitly type https:// in the address. What's the possible cause here?
IIS does not support .htaccess file.so I suggest you use iis URL rewrite module and add a rule using that.
for that, you need to install the URL rewrite module by using the web platform installer or from this below link.
https://www.iis.net/downloads/microsoft/url-rewrite
than add below code in your config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
for more detail you could refer this below link:
https://stackoverflow.com/a/55531655/11147346
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourwebsite.com/$1 [R,L]
try that
or use cloudflare to cache and perfomace your project
https://support.cloudflare.com/hc/en-us/articles/115000219871-Troubleshooting-redirect-loop-errors
https://community.cloudflare.com/t/cloudflare-flexible-ssl-is-it-worth-it/28043
https://developers.cloudflare.com/ssl/origin/ssl-modes/

how convert htaccess deny rule to IIS url rewrite rule

I am converting .htaccess file to rewrite rules in IIS to convert from http to https,
all the rules are being converted which starts with RewriteCond but deny from all is not converted
I want to convert this htaccess rule to IIS rewrite rule
<Files ~ "\.log$">
Deny from all
</Files>
Is there any other way to convert it? or I doing something wrong.
to convert this rule you could use the request filtering feature of iis or URL rewrite.
<configuration>
<security>
<requestFiltering>
<fileExtensions>
<add fileExtension=".log" allowed="false" />
</fileExtensions>
</requestFiltering>
</security>
</system.webServer>
</configuration>
using url rewrite rule:
<rule name="Protect files and directories from prying eyes" stopProcessing="true">
<match url="\.log$" />
<action type="CustomResponse" statusCode="403" subStatusCode="0" statusReason="Forbidden" statusDescription="Access is forbidden." />
</rule>
for more detail please refer this link:
https://learn.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-applications-on-iis/translate-htaccess-content-to-iis-webconfig#request-filtering

How to convert this htaccess to iis web.config?

Why when i convert .htaccess automatically with IIS web.config via IIS Manager > Rewrite URL this show me 404 error
How to convert this manually to web.config?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(install)($|/) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This is the result using IIS Manager (web.config)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(install)($|/)" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/app/webroot/" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/app/webroot/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
What's the Sub-status code of 404 error? Because 404.0 and 404.19 is different error. You could find it in C:\inetpub\logs\LogFiles.
It looks like the rule has been transformed correctly.
Rule1 will scan incoming URL domain.com/install/ or domain.com/install and prevent these URL from being rewritten.
Rule2 will rewrite domain.com to domain.com/app/webroot/
Rule3 will rewrite Any incoming rule for example. domain.com/a.aspx to domain.com/app/webroot/a.aspx.
So are these rules achieve your requirement?
To promise rewrite could be processed without ensure
1.Please ensure the request has been delivered to your IIS server. Sometimes networking issue would route the request to another server and return 404 error.
2.Please ensure the target URL like domain.com/app/webroot/ or domain.com/app/webroot/(.*) can be accessed without any issue.

.htaccess don't work in azure

i try to put my .htaccess (that work on linux server) in my azure end not work.
I think the issue is that I had to use web.config structure, so I try also this, but now http return 500.
My .htaccess is:
RewriteCond %{REQUEST_URI} r\/([\w\d]+)?\/([\w\d]+)$
RewriteRule r\/([\w\d]+)?\/([\w\d]+) php/Router.php?called_class=$1&called_method=$2 [L,QSA]
I've solved.
This is web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rule1" stopProcessing="true">
<match url="r\/([\w\d]+)?\/([\w\d]+)" />
<action type="Rewrite" url="/php/Router.php?called_class={R:1}&called_method={R:2}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

How to rewrite htaccess RewriteCond http_cookie into web.config alternative?

I'm having a hard time rewriting the following RewriteCond into web.config alternative and would appreciate any help.
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in_.*$
Using URL Rewrite module in IIS 7, rewrite rules with conditions would look something like this:
<rules>
<rule name="something">
<match ... />
<conditions>
<add input="{HTTP_COOKIE}" pattern="^.*wordpress_logged_in_.*$" />
</conditions>
<action ... />
</rule>
</rules>
You can also use the URL Rewrite GUI interface in a local IIS 7 install to help build your rewrite rules, and then open the corresponding web.config to see how it should look.

Resources