IIS URL rewrite 404 errors - iis

I am running the config below to rewrite for example
http://www.phusewebdesign.co.uk/blog/Get-in-touch
to
http://www.phusewebdesign.co.uk/index.php?s=blog&p=Get-in-touch
but I get a 404 error, I am missing something simple I am sure but can't fimd the answer on google or here
thanks
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?s={R:1}&p={R:2}" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?p={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

IIS rewrite rule. Can't get it to work. It gives 'redirect multiple times' error

I need to redirect the user every time he enters subdomain.mywebsite.com to subdomain.mywebsite.com/app
I tried to do the following
<rewrite>
<rules>
<rule name="MyRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^subdomain.mywebsite.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.mywebsite.com/app" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it gives me 'redirect multiple times' error
Please advice
UPD: I also need it to work for https
https://subdomain.mywebsite.com
So every time the user opens subdomain.mywebsite.com with HTTP or HTTPS it should forward to https://subdomain.mywebsite.com/app
That's because your rule also works for https://subdomain.mywebsite.com/. So it keep redirecting to itself.
Please add a condition pattern {HTTPS}=off to only redirect http request.
<rule name="MyRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^subdomain.mywebsite.com$" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://subdomain.mywebsite.com/app" redirectType="Permanent" />
</rule>
5/7Edit:
Please try this rule
<rule name="MyRule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^subdomain.mywebsite.com$" />
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="https://subdomain.mywebsite.com/app" redirectType="Permanent" />
</rule>
<rule name="My Rule2" enabled="true" stopProcessing="true">
<match url="^app(/)?$" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^subdomain.mywebsite.com$" />
<add input="{HTTPS}" pattern="^on$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="https://subdomain.mywebsite.com/app" />
</rule>
<rule name="MyRule3" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^subdomain.mywebsite.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.mywebsite.com{REQUEST_URI}" />
</rule>

Rewrite rule for a folder/page name without an extension

I have a rewrite rule that doesn't seem to be working as I anticipated. I'm redirected to a 404. It seems to only work if I have an extension on the page.
/folder/page.html works but /folder/page doesn't.
<rule name="preact" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(docs)" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>

IIS Rewrite problems - catching all .php files

In a folder I have several .php files (more than 10)
aaa.php
bbb.php
ccc.php
ddd.php
index.php
What I'm trying to achieve is properly re-writing them so as to have friendly URLs. The rewrites are:
www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1
www.domain.com/data/bbb -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/ -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/chapter1 -> www.domain.com/data/bbb.php?c=chapter1
...
By using the following rules I achieve what I want for single php files
<rule name="Friendly1" stopProcessing="true">
<match url="^aaa$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" stopProcessing="true">
<match url="^aaa/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" stopProcessing="true">
<match url="^aaa/(.+)\.(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.{R:2}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
<match url="^aaa/(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php?c={R:1}" appendQueryString="false" />
</rule>
That works. However it's not efficient and it's error prone copypasting those rules for every php file in the folder.
So, I thought of exchanging the aaa with (.+) in the rules and it doesn't work for some cases.
<rule name="Friendly1" 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="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" 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="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" 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="{R:1}.php/{R:2}.{R:3}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
<match url="^(.+)/(.+)$" ignoreCase="true"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php?c={R:2}" appendQueryString="false" />
</rule>
It works for the following cases:
www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php
It doesn't work for the following cases:
www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1
For which cases I get a "not found".
Am I doing something wrong? How can I solve this ?
IIS = 7.5+
Thank you in advance.
How about this...
<rewrite>
<rules>
<rule name="Friendly1" stopProcessing="true">
<match url="^data/([-a-zA-Z0-9]*)/[-a-zA-Z0-9]*)$" ignoreCase="true" />
<action type="Rewrite" url="data/{R:1}.php?c={R:2}" />
</rule>
<rule name="Friendly2" stopProcessing="true">
<match url="^data/([-a-zA-Z0-9][-a-zA-Z0-9]*)$" ignoreCase="true" />
<action type="Rewrite" url="data/{R:1}.php" />
</rule>
<rule name="Friendly3" stopProcessing="true">
<match url="^data(/.*)$" ignoreCase="true" />
<action type="Rewrite" url="data/index.php" />
</rule>
</rules>
</rewrite>

IIS rewrite rule for processing all URLs except given

Here is the IIS rewriting rule for some URLs to process:
<rule name="rule" stopProcessing="true">
<match url="^(word|world|hero|about)[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
How can I create an opposite rule? For example, I want to process all URLs except "/special", "/escape". This:
<rule name="rule" stopProcessing="true">
<match url="^(?!(special)&?!(escape))[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
does not work. "/special" and "/escape" URLs are processed as they should, but other URLs give me 404 pages.
You have to use the negate attribute in this case.
Your rule will become:
<rule name="rule" stopProcessing="true">
<match url="^(special|escape)/?$" negate="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>

Rewrite rule is not working proper in IIS for phpfox

**
I change my rewrite rule to change my website link from www.mydomain.com/index.php?do=/blog to www.mydomain.com/blog after this when i tried the below code, it won't let me change any page, like if i want to go for www.mydomain.com/blog, i just got stucked at www.mydomain.com, means redirect to my same root page,
what changes should i made for this please help. i am a newbei
my code is**
`<rule name="Redirect index.php" stopProcessing="true">
<match url="index\.php/(.*)" />
<action type="Redirect" url="{R:1}" appendQueryString="false" />
</rule>
<rule name="Rewrite index.php">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>`
Based on this forum post about .htaccess rewrites, you need to rewrite to:
index.php?do=/{R:1}
You are missing the ?do= part in the middle.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1">
<match url="^file/pic/photo/([0-9]+)/([0-9]+)/([A-Za-z0-9]{32}+)\-(.*?)_([0-9]*?)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="file/pic/photo/{R:1}/{R:2}/{R:3}_{R:5}.{R:6}" />
</rule>
<rule name="Imported Rule 2">
<match url="^file/pic/photo/([0-9]+)/([0-9]+)/([A-Za-z0-9]{32}+)\-(.*?)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="file/pic/photo/{R:1}/{R:2}/{R:3}.{R:5}" />
</rule>
<rule name="Imported Rule 3">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://www.%" />
</rule>
<rule name="Imported Rule 4" 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="Redirect" redirectType="Permanent" url="{URL}/" />
</rule>
<rule name="Imported Rule 5">
<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?do=/{R:1}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 6">
<match url="^file/pic/photo/(.*)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="static/image.php?file={R:1}&ext={R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources