IIS 7 URL Rewrite Rule Assistance - url-rewriting

I would like to take the following URL
http://MYSERVER/API_Tasks/V1/controller/task.php?taskid=2
and rewrite it to:
http://MYSERVER/API_Tasks/V1/tasks/2
I have the following rule created but it does not seem to work
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^tasks/([0-9]+)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="controller/task.php?taskid={R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
original Apache .htaccess rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tasks/([0-9]+)$ controller/task.php?taskid=$1 [L]

The issue was actually caused by the rule in IIS being placed on the API_Tasks directory and not the API_Tasks/V1 directory. Adding the URL rule to the V1 fixed the issue.

Related

Routing web.config to index.php within folder

I have no experience working with windows web servers so i'm a bit lost. I'd like to setup a folder that routes all urls to index.php. I have attempted a few different configurations without any of them working.
How I would do it in .htaccess:
RewriteEngine on
RewriteBase /Software
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /folder/index.php?/$1 [NC,QSA,L]
My attempts with web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect To Index" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/folder/index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Place the below rules in webconfig file under the Software directory. If the webconfig file doesn't exists, create it. the rules in it can be recognized by IIS.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{R:1}" pattern="^(index\.php|images|robots\.txt)" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/folder/index.php?/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
IIS rewrite rules are designed with a different philosophy. It doesn’t support the RewriteBase command. We just place the webconfig file in the corresponding folder. The rest URL rules will work in the specific directory.
In addition, the rest URL rules could be automatically converted into IIS Rewrite Rules by using the below IIS features.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/importing-apache-modrewrite-rules
About how to convert the RewriteBase command.
https://forums.iis.net/t/1162790.aspx
Feel free to let me know if there is anything I can help with.

URL rewrite images to another domain without redirect

I have main domain and I want to rewrite images form another one to main without redirect
Here is my code:
RewriteCond %{HTTP_HOST} ^www.mydomain\.eu$ [NC]
RewriteRule ^images/(.*)$ https://www.mydomain.cz/images/$1 [NC]
It always does 302 redirect.
Is there any way how to do this? I am using ISAPI_Rewrite, but I tried IIS 10 rewrite too, but I am not familiar with that and I wasnt successful at all
Thanks
According to your description, I suggest you could try to below use url rewrite rule in the IIS.
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^images/(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www.mydomain\.eu$" />
</conditions>
<action type="Rewrite" url="https://www.mydomain.cz/images/{R:1}" />
</rule>

reproduce multiple apache rewrites in IIS 8 with URL Rewrite

My previous web server ran apache with rewrites in each site's .conf file. I wouldn't even consider myself an intermediate rewrite user so this may not be ideal. I'm trying to reproduce this:
# except for requests for URL-path /abc/abc_system<whatever>
RewriteCond %{REQUEST_URI} !^(/abc/abc_system|/abc/themes|/abc/admin.php)
RewriteCond %{HTTP_HOST} ^([^.]+\.)*abctest\.example\.com [NC]
RewriteRule ^/abc/(.*)$ /abc/index.php/abc/$1 [L]
# one
RewriteRule ^/one/(.*)$ /one/index.php/one/$1 [L]
# two
RewriteRule ^/two/(.*)$ /two/index.php/two/$1 [L]
# three
RewriteRule ^/three/(.*)$ /three/index.php/three/$1 [L]
within IIS 8 using URL Rewrite.
<rewrite>
<rules>
<clear />
<rule name="abc ee" stopProcessing="false">
<match url="^abc/(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^(/abc/abc_system|/abc/themes|/abc/admin.php)" ignoreCase="false" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^.]+\.)*abctest\.example\.com" />
</conditions>
<action type="Rewrite" url="/abc/index.php/abc/{R:1}" logRewrittenUrl="true" />
</rule>
<rule name="abc one" stopProcessing="false">
<match url="^/one/(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Rewrite" url="/one/index.php/one/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
The first rule is working but the second one ("abc one") isn't, so I haven't even bothered adding the rules for "two" and "three" yet.
Basically the urls look something like this:
example.com/abc -> example.com/abc/index.php/abc (working)
example.com/one -> example.com/one/index.php/one
example.com/two -> example.com/two/index.php/two
example.com/three -> example.com/three/index.php/three
What am I doing wrong that the second rule isn't working?
Once that's fixed, is repeating it the best way to go about it or is there a more efficient way to condense them down?
Many thanks!
you can use "OR" to catch the two/three cases and will be faster, I tested the following rule and it worked fine:
<rule name="RewriteOneTwoThree" stopProcessing="true">
<match url="^(one|two|three)/(.*)$" ignoreCase="false" />
<action type="Rewrite" url="/{R:1}/index.php/{R:1}/{R:2}" />
</rule>

How to convert this apache mod rewrite to iis web.config

I am using apache mod rewrite and i have a .htaccess rewrite statement. I would like to change over to use IIS server and would like to know if any one can give me advice on how to convert my existing .htaccess statement to iis web.config.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
Expert Advice appreciated.
For IIS 7 you would use <system.webServer><rewrite> section. There is an excellent article on translating .htaccess to web.config here
<rewrite>
<rules>
<rule name="Your Rule" 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="index.php?url={R:1}" />
</rule>
</rules>
</rewrite>
Resolved. Download url rewrite from Microsoft web platform installer. Url rewrite module can auto create the web.config when you specify the rules via their ui.

Using mod_rewrite to redirect non-existent subdomain to home page

I have searched all over this site and the web and still I cannot get this right. I have many URLs like this: http://archives.cancunandrivieramaya.com/archives/cancun.cgi/md/read/id/XXXX that I want to redirect to the home page http://www.cancunandrivieramaya.com/. The subdomain no longer exists. How do I write the mod-rewrite to redirect the subdomain with wildcard characters at the end to the homepage? This is one of the 100's that don't work :(
RewriteCond %{HTTP_HOST} !^www\.cancunandrivieramaya\.com [NC]
RewriteCond %{HTTP_HOST} ^archives\.cancunandrivieramaya\.com([a-z0-9]+)\
RewriteRule (.*) http://www.cancunandrivieramaya.com/[L,QSA]
Any help would be much appreciated.
I got it! Thanks to this article by Altaf Khatri http://www.altafkhatri.com/Technical/Configure/How-to-publish-or-host-subdomain-on-winhostcom/Solution-resolution I was able to follow his step-by-step instructions for setting up the subdomain on WinHost, then I added this rule to the web.config file:
<rule name="archive redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.archives\.cancunandrivieramaya\.com$" />
</conditions>
<action type="Redirect" url="http://www.cancunandrivieramaya.com/" />
</rule>
<rule name="archives 2" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?archives\.cancunandrivieramaya\.com$" />
<add input="{PATH_INFO}" pattern="^/archives/($|/)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.cancunandrivieramaya.com/" />
</rule>
Works perfectly!

Resources