Converting .htaccess to web.config - asp.net

I am currently in the process of moving from parallels plesk for centos to parallels plesk for windows. The only snag i've hit is I realized .htaccess won't work anymore.
Here was my .htaccess:
options -indexes +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(verify)/(.*)/([a-zA-Z0-9_-]+)$ validate_account.php?usr=$3&hash=$4 [QSA,L]
RewriteRule ^(post)/([a-zA-Z0-9_-]+)$ news.php?post=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^uploads/avatars/(.+)$ ../uploads/avatars/$1 [L,NC]
Redirect 301 /signup /register
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
errordocument 404 /404.php
errordocument 403 /404.php
errordocument 500 /404.php
allow from all
#deny from all
#allow from 98.115.168.25
deny from 162.144.91.203 #attempted brute force
deny from 74.208.100.64 #attempted brute force
deny from 185.57.82.25 #tried to ddos
Here is the web.config I generated with a website:
<rule name="rule 1F">
<match url="^(.*)$" />
<action type="Rewrite" url="/{R:1}.php" />
</rule>
<rule name="rule 2F" stopProcessing="true">
<match url="^(verify)/(.*)/([a-zA-Z0-9_-]+)$" />
<action type="Rewrite" url="/validate_account.php?usr={R:3}&hash={R:4}" appendQueryString="true" />
</rule>
<rule name="rule 3F" stopProcessing="true">
<match url="^(post)/([a-zA-Z0-9_-]+)$" ignoreCase="true" />
<action type="Rewrite" url="/news.php?post={R:2}" appendQueryString="true" />
</rule>
<rule name="rule 4F" stopProcessing="true">
<match url="^uploads/avatars/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="/../uploads/avatars/{R:1}" />
</rule>
<rule name="rule 5F">
<match url="(.*)" />
<action type="Rewrite" url="/https://%{HTTP_HOST}%{REQUEST_URI}" />
</rule>
<rule name="rule 6F" stopProcessing="true">
<match url="^" />
<action type="Rewrite" url="/http://%1%{REQUEST_URI}" />
</rule>
It gives me 500 internal server error whenever I try accessing the site, any help would be appreciated.

Try this and let me know
<rule name="rule 1X">
<match url="^(.*)$" />
<action type="Rewrite" url="/{R:1}.php" />
</rule>
<rule name="rule 2X" stopProcessing="true">
<match url="^(verify)/(.*)/([a-zA-Z0-9_-]+)$" />
<action type="Rewrite" url="/validate_account.php?usr={R:3}&hash={R:4}" appendQueryString="true" />
</rule>
<rule name="rule 3X" stopProcessing="true">
<match url="^(post)/([a-zA-Z0-9_-]+)$" ignoreCase="true" />
<action type="Rewrite" url="/news.php?post={R:2}" appendQueryString="true" />
</rule>
<rule name="rule 4X" stopProcessing="true">
<match url="^uploads/avatars/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="/../uploads/avatars/{R:1}" />
</rule>
<rule name="rule 5X">
<match url="(.*)" />
<action type="Rewrite" url="/https://%{HTTP_HOST}%{REQUEST_URI}" />
</rule>
<rule name="rule 6X" stopProcessing="true">
<match url="^" />
<action type="Rewrite" url="/http://%1%{REQUEST_URI}" />
</rule>

After installing the free version of ISAPI Rewrite I was able to use .htaccess with IIS 7.

Related

unable to convert .htaccess to web.config

For this pc of code
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
i am using
<rule name="rule 1A" stopProcessing="true">
<match url="." />
<action type="Rewrite" url="/index.php" />
</rule>
but not work... why ?
This config is solved my problem
<?xml version="1.0"?>
<rewrite>
<rules>
<rule name="DeduplicateSlashes" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
<rule name="CleanRouting" stopProcessing="true">
<match url="^.*$" ignoreCase="false" />
<conditions>
<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="?id={R:0}&{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<!-- Double escaping is needed for URLs with '+', e.g. for the account page for a username with a space. -->
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>

Rewrite rule for redirect url on different domain

I am trying to point subdomain.domain.net to www.subdfgomain.com or whaterver , i try with htaccess rule but not getting result , Please help !!
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/projectname/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/projectname/index.php [L]
RewriteCond %{HTTP_HOST} ^projectname.domain.net
RewriteRule (.*)$ http://www.another.com/$1 [R=301,L]
</IfModule>
# END WordPress
You need to keep redirect before rewrite rules. Try this code:
RewriteEngine On
RewriteBase /project/projectname/
RewriteCond %{HTTP_HOST} ^projectname\.domain\.net$ [NC]
RewriteRule ^ http://www.another.com%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/projectname/index.php [L]
i have managed through web.config , and now it's working .
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WordPress Rule 1" stopProcessing="true">
<match url="^index\.php$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="WordPress Rule 2" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
<action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
</rule>
<rule name="WordPress Rule 3" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
<action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
</rule>
<rule name="WordPress Rule 4" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="WordPress Rule 5" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
<action type="Rewrite" url="{R:2}" />
</rule>
<rule name="WordPress Rule 6" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
<action type="Rewrite" url="{R:2}" />
</rule>
<rule name="WordPress Rule 7" stopProcessing="true">
<match url="." ignoreCase="false" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Redirect url without .html type to html page

I have a godaddy (windows) hosting account and I want to redirect:
www.example.com/folder/some to www.example.com/folder/some.html
I tried adding this Web.config file, but cannot find what rule to add for redirecting the html
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add www" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^www\.(.+)$" />
<add input="{HTTP_HOST}" negate="true" pattern="^[^.]*dav.*" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Try using the following code in your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Zend Framework 2 IIS url rewrite

I moved my zend project from Apache to IIS 7 and set up URL Rewrite.
The home page shows just fine, but the css and javascript aren't loading.
Here is my rewrite scripts
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="public/index.php" />
</rule>
<rule name="Imported Rule 1-1" stopProcessing="true">
<match url="\.(js|ico|txt|gif|jpg|png|css)$" ignoreCase="false" negate="true" />
<action type="Rewrite" url="public/index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Here are the original mod_rewrite rules
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
Any suggestions?
After combing through Zend2 documentation, I found this example. Works perfectly! I hope this helps someone else out.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}"
matchType="IsFile" pattern=""
ignoreCase="false" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
pattern="" ignoreCase="false" />
</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>

how to use matches in conditions tag in Web.config file on IIS7?

I wanna convert this apache .htaccess lines to web.cofig:
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=302]
with these codes I can redirect all request with www. even if my website has too many domains.
I research and found my answer. for use variables in conditions we can use {C:1}. that 1 is for first match. and my ecxact answer for this converting is:
<rules>
<rule name="http Redirect" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="https Redirect" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
<add input="{SERVER_PORT_SECURE}" pattern="^0$" negate="true" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
That is equal to:
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteCond %{HTTPS} ^off$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,QSA,R=302]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteCond %{HTTPS} ^on$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,QSA,R=302]
Good Luck

Resources