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

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.

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 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.

Mod_rewrite in Azure

I was write simple .htaccess and test it on Windows Azure Web Sites, but mod_rewrite didn't work there. Why? How I can reconfigurate my Azure?
RewriteEngine on
AddDefaultCharset utf-8
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test.html$ test.php?%{QUERY_STRING} [L]
.htaccess file is not recognizible by Azure Web Sites.
Azure Web Sites run on Microsoft IIS.
IIS has a URL Rewrite module, very similar to mod_rewrite for Apache. You can configure URL Rewrite rules by having a web.config file in your site root folder.
Follow the Creating Rewrite Rules article and scroll down to the "View the rule in Config file" to get an idea what it looks like.
Your rules defined, will look like this in web.config (and will most probably work as expected):
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^test.html$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<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="test.php?{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>

Rewrite rule conversion to web.config

Could anyone tell me how what the conversion of the below rewrite would be for a web.config file?
RewriteEngine On
RewriteRule ^/(credits|content)/?$ switch.php?view=$1
Or if there is any online conversion tool that would be great.
Thanks
EDIT
For example:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Would be:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
I can't tell you exactly how to convert the rules you have provided (I'm not great with .htaccess or web.config unfortunately), but there is a handy tool built into IIS Manager that enables you to convert .htaccess rewrite rules into IIS URL rewrite rules.
It's usage is described here:
http://www.iis.net/learn/extensions/url-rewrite-module/importing-apache-modrewrite-rules
This would convert 2 kind of incoming URLs like this:
http[s]://yoursite.com/credits => switch.php.view=credits
http[s]://yoursite.com/credits/ => switch.php.view=credits
http[s]://yoursite.com/content => switch.php.view=content
http[s]://yoursite.com/content/ => switch.php.view=content
Nb: this rule doesn't keep parameters in the URLs e.g.:
http[s]://yoursite.com/credits?param1=aa&param2=bb => switch.php.view=credits
http[s]://yoursite.com/credits/?param1=aa&param2=bb => switch.php.view=credits
http[s]://yoursite.com/content?param1=aa&param2=bb => switch.php.view=content
http[s]://yoursite.com/content/?param1=aa&param2=bb => switch.php.view=content

Anyone get CakePHP 2 to work on IIS 5.1 with Helicon ISAPI_rewrite?

I'm trying to get CakePHP 2.0 to work on my Win2k laptop at home but have been stymied by mod rewrite issues. For previous versions of Cake (1.2, 1.3) I used ISAPI_rewrite with the following rule (my app is in a virtual folder 'cake'):
RewriteEngine on
RewriteBase /cake
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
However this doesn't seem to work on CakePHP 2.0. If I enter the base directory 'localhost/cake/' I get the standard Cake welcome screen (tmp dir status, etc...) and it says everything is configured correctly. If I type in 'localhost/cake/widgets/index' it churns a bit and then just goes back to the standard screen that says everything is OK. No errors, nada.
This isn't the first time I've had problems with IIS, but I'm stuck with this laptop for a while longer. Any help would be appreciated.
as I already answered here:
http://groups.google.com/group/cake-php/browse_thread/thread/c266c5227b16dab7
it should read:
index.php?/$1
This worked for me:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect plugin resources" stopProcessing="true">
<match url="^(.*)/(ico|img|css|files|js)(.*)$" />
<action type="Rewrite" url="app/plugin/{R:1}/webroot/{R:2}{R:3}" appendQueryString="false" />
</rule>
<rule name="Redirect static resources" stopProcessing="true">
<match url="^(ico|img|css|files|js)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources