Rewrite rule conversion to web.config - .htaccess

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

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>

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.

IIS Rule Convert To .htaccess Rule

I need some help writing a .htaccess rule that:
Removed the .php extension from a file name
Rewrites variables.
So, a url like www.example.com/contact-us/456/789 would be interpereted by the server as www.example.com/contact-us.php?a=123&b=456&c=789
The following IIS web.config rule works a treat (using a rewrite map):
<rule name="Dynamic Rewriting" stopProcessing="true">
<match url="^([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{DynamicRewriting:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/{C:1}.php?page={C:1}&a={R:2}&b={R:3}&c={R:4}" />
</rule>
<rewriteMap name="DynamicRewriting">
<add key="contact-us" value="contact-us" />
</rewriteMap>
Can anyone show me the .htaccess rule I need?
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^/?([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$ /$1.php?page=$1&a=$2&b=$3&c=$4 [L,QSA]

Resources