IIS Rewrite rule with QueryString in Rewrite - iis

I'm having a problem with an IIS rewrite rule. We have many set up and working in our web.config file. I am trying to modify one of them. Apologies for the level of this question. I'm not experienced in this topic.
The current rule is:
<rule name="my-rule-test" stopProcessing="true">
<match url="^my-page.asp" />
<action type="Rewrite" url="/my-domain/my-actual-page.asp" />
</rule>
This rule works fine. I'm now trying to simply add a QueryString to the Rewrite page as follows:
<rule name="my-rule-test" stopProcessing="true">
<match url="^my-page.asp" />
<action type="Rewrite" url="/my-domain/my-actual-page.asp?aS=h" />
</rule>
This rule fails and trips the server over to the page we have set up in web.config here:
<system.web>
<customErrors defaultRedirect="c:\test.html" mode="On">
</customErrors>
</system.web>
Can somebody please let me know what I'm doing wrong?
Thanks,
J

Your formatting is not correct:Please refer to this.
<rules>
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

Related

IIS 7 Redirect to new domain

Good morning,
I am trying to create a Rewrite rule in IIS to redirect a specific page to a new url however when I apply the rule no redirect happens what am I missing? Code:
</rule>
<rule name="Redirect" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/)?$" />
<add input="{HTTP_HOST}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" />
</rule>
Try adding REQUEST_URI to the pattern match
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
match url may also work better as
<match url="^(.*)$" />
also make sure the rule is enabled, your version has enabled="false" on it
Here is my full rule
<rule name="domain1/server/sdk/" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" redirectType="Permanent" appendQueryString="false" />
</rule>
Excellent thank you very much Mike, I did disable my rule after testing it and it did not work which is why it shows enabled=false however I revised the rule per your advice and that worked for me! Incase this is helpful for anyone else in the future my full rule is:
<rules>
<remove name="Portal Redirect" />
<rule name="(rule name)" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Redirect" url="Domain2/rest/" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/rest/" />
</conditions>
</rule>

How to set a condition in IIS UrlRewrite to check if the URL is valid

I have an issue with setting an condition for my IIS URLRewrite to set it so when the entered url is not valid then it takes you to a set url. for instance you type url: example.local/current_work that would be a valid url so it shows the page however for url : example.local/this_doesnt_exist it will redirect you back to the example.local/current_work etc...
Hope I am clear on this one.
<rewrite>
<rules>
<rule name="Test" stopProcessing="true">
<match url="^scottishtrunkroadsse.amey.local/([_0-9a-z-]+)" negate="false"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="current-works/" />
</rule>
</rules>
<rewrite>
Thank you for any help.
I am using an Umbraco website but I don't think that change anything as the IIS url should be working in the same way.
Thank you in advance.
This rule will redirect example.local/this_doesnt_exist to example.local/current_work:
<rule name="Test" stopProcessing="true">
<match url="^this_doesnt_exist$" />
<action type="Redirect" url="/current_work" />
</rule>

IIS 7.5 URL Rewrite Module - Rewrite Maps with variables

I have several pages on my site that have long and unreadable URLs that I'd like to be able shorten. The problem I'm having is appending query strings with variable values.
For example:
"www.example.com/dir1/dir2/filename.php" shortens to "www.example.com/file".
"www.example.com/dir1/dir2/filename.php?id=2" would be "www.example.com/file/2".
"www.example.com/dir1/dir2/filename.php?id=2&alt=6" would be "www.example.com/file/2/6".
The values of 'id' and 'alt' are then used by our page to access information in our database, which determines the contents of the page. These values can change, and there is no set amount.
Right now I've got the first example working fine using the following rewrite rules:
<rewrite>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
</outboundRules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/file" value="/dir1/dir2/filename.php" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
</rewrite>
But I haven't been able to find anything that would allow the URLs to contain variables. Everything I've seen has used static rewrites like my current solution is using, and I can't find anything about allowing arbitrary parameters.
EDIT:
Found a better solution that doesn't use a Rewrite Map. I had attempted a simliar rule previously, but due to the IIS setup on our testing environment it wasn't working as expected. This version should work for most people.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
Using this rule I was able to avoid using a rewrite map altogether. I had attempted to use something like this originally, but due to the setup of our test environment it wouldn't work without some weird tweaks. This version should work in all normal environments.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
EDIT:
I was also able to get the original version with a rewrite map working for anyone interested.
<rule name="Rewrite Map with Variables" enabled="true">
<match url="^(.+?)/?/(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{ProductMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/dir1/dir2/filename.php?id={C:0}&other={R:2}" appendQueryString="true" />
</rule>

Htaccess and CakePHP 2 on windows IIS7

I was having problems trying to configure the mod rewrite over windows and II7:
CakePHP 2.2.2 not working on Windows IIS7 but finally i could import the htaccess in order to create the web.config file for IIS7.
The thing is:
Now that file has been created in cakephp folder and i can access to the main page BUT it has not been created inside app/ or app/webroot in which you can find 2 more .htaccess files.
Now, i can not access any other view than the main site, it shows a 404 page not found error and i am pretty sure that it is because it is not getting those .htaccess files on the web.config.
My cakephp web.config file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Although the CakePHP documentation tells you to add a different code (which makes the views work but no styles are loaded. And the home page don't work.)
http://book.cakephp.org/2.0/en/installation/advanced-installation.html#url-rewrites-on-iis7-windows-hosts
When trying to access a CSS file from the URL i get this message:
Missing Controller
Error: CssController could not be found.
Error: Create the class CssController below in file: app\Controller\CssController.php
<?php
class CssController extends AppController {
}
Any idea? Working with Cakephp over windows is driving me crazy...
I should mention that my response is an improvement on the solution posted here: http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/
A simpler and more flexible fix would be to get rid of /{Path_To_CakePHP_Directory}/ altogether, including the forward slash (/). By keeping the paths relative, your project folder becomes a lot more mobile. Here's what the web.config would look like:
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear/>
<rule name="Imported Rule 0" stopProcessing="true">
<match url="^(img|css|files|js)(.*)$"></match>
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false"></action>
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</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>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Ok, finally i get it work using this web.config i found on this site:
http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/
I just changed the /{Path_To_CakePHP_Directory}/ for /.

IIS URL Rewriting - Rule captures despite file existing

I've got a rule that rewrites URLs unless a physical file exists (so static files can be returned) - however for some reason the rules get rewritten anyway, to much frustration.
Here's my Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Front Page">
<match url="/?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="/Home/FrontPage" />
</rule>
<rule name="Map Everything" 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="EntryPoint.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
Ah, it turns out the "Front Page" rule was the problem, and the rule "/?" was matching everything when I should have used "^$" instead.
After fixing that, the IsFile/IsDirectory rules were being respected.

Resources