I'm trying to get an iis7 url rewrite to work on a query in the home directory. The end goal is to get it to append index.php to the beginning of the query string. Everything I try ends in a 500 error. What am I doing wrong?
<rule name="post preview fix" patternSyntax="ECMAScript">
<match url="^\?p=([0-9]+)&preview=true" />
<action type="Rewrite" url="index.php?p={R:1}&preview=true" />
</rule>
Since you want to base your rule on the query string, you have to use the conditions.
Something like this should do:
<rule name="post preview fix" stopProcessing="true">
<match url="^index.php$" negate="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="^p=([0-9]+)&preview=true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
If the requested page is not index.php and the query string matches ^p=([0-9]+)&preview=true, then the rwrite is triggered.
The appendQueryString option is set to true by default so no need to set it.
Related
I am struggling getting an IIS rewrite rule working.
The old urls look like:
https://wwww.testserver.com/package.aspx?TrackingNumber=number
The new web app is expecting the urls in this format:
https://wwww.testserver.com/package/number
Currently I have the following rule setup, which is not working.
<rule name="Rewrite to new Package site" stopProcessing="true">
<match url="^package\.aspx\?TrackingNumber=([_0-9a-z-]+)" />
<action type="Redirect" url="https://www.testserver.com/package/{R:1}" logRewrittenUrl="true" />
</rule>
The interesting part is, if I remove package.aspx\? from the rule, urls like these https://wwww.testserver.com/oldwebsite/TrackingNumber=number are getting matched.
In my test calls I replaced package.aspx with package.html and these urls are not getting matched as well. It looks like IIS ignores urls with filenames in the url.
You could use below url rewrite rule to redirect https://wwww.testserver.com/package.aspx?TrackingNumber=number to https://wwww.testserver.com/package/number:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="package(.aspx)" />
<add input="{QUERY_STRING}" pattern="TrackingNumber=(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:2568/package/{C:1}" appendQueryString="false" />
</rule>
Note: You could modify hostname as per your requirement.
Regards,
Jalpa
I am new to the IIS Rewrite rule and trying to create a rule to replace part of url
e.g.www.abc.com/assets/global/xyz.jpg
should redirect to www.abc.com**/en/globalassets/**assets/global/xyz.jpg
I fiddle around with the following rule but no success
<rule name="url replace">
<match url="^(.com/assets/global/)" />
<action type="Rewrite" url=".com/en/globalassets/assets/global/{R:2}" />
</rule>
According to your description, I have tested on my side , you could use urlrewite rule as below:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url="assets/global/(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="en/globalassets" negate="true" />
</conditions>
<action type="Redirect" url="http://{domain}/en/globalassets/assets/global/{R:1}" />
</rule>
Firstly, we couldn't add value like **.com in match url, because this part could only catch path of the url.
You could see this is a url structure:
http(s)://httphost/path?querystring.
You could only get it in conditions tag but not pattern.
Then you should add condition to check the request URL match "en/globalassets" or not to avoid running redirect rule once and once again.
I have tried multiple combinations so far but no luck. I have a URL like below.
https://teams.company.com/Search/pages/results.aspx?url=https://teams2017.company.com/sites/hrdepartment
I want to create a rule that will change the query string value from teams2017 to teams only, like below.
https://teams.company.com/Search/pages/results.aspx?url=https://teams.company.com/sites/hrdepartment
I am using IIS 8.5 with IIS Rewrite rule installed.
You rule should be like that:
<rule name="teams2017 to teams" stopProcessing="true">
<match url="^Search/pages/results.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)teams2017(\.company\.com.*)" />
</conditions>
<action type="Redirect" url="{R:0}?{C:1}teams{C:2}" appendQueryString="false" />
</rule>
I'm new to the world of IIS in terms of redirects and have been tasked with setting up a redirect for a portion of our site.
The URL I need to redirect is brandview.auditedmedia.com and I need it
to go to https://auditedmedia.com/data/media-intelligence-center/brand-view
The trick is we can have a URL with a QueryString such as
brandview.auditedmedia.com/memberNumber=423524 and that URL should NOT
be redirected.
Also, the scheme of brandview.auditedmedia.com does not matter. Both
HTTP and HTTPS should redirect.
So I'm not sure should I be doing this in IIS or as a RouteMap in the app?
If I should be doing this in IIS can someone point me to either an article or give an example of what I need to do?
Thanks
Bob
You need to add URL rewrite rule on the server level. Use conditions to prevent redirection for requests with non-empty query string string.
For your case it is necessary to add the following rule:
<rule name="brandview redirect" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^brandview\.auditedmedia\.com$" negate="false" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" appendQueryString="false" redirectType="Permanent" />
</rule>
There is detailed instruction on your question. Note, the rule must be created in system.webServer/rewrite/globalRules section of %windir%\system32\inetsrv\config\ApplicationHost.config file.
I got the pattern fixed (see below) and though all the pattern matching works properly in URL Rewrite Module (using test button and putting in different URLs), it still isn't working properly when I attempt to go to the site. The server just continues with serving the page and not redirecting.
<rewrite>
<globalRules>
<rule name="brandview redirect" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(https?:\/\/)?brandviewdev\.auditedmedia\.com$" negate="false" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" redirectType="Permanent" appendQueryString="false" />
</rule>
</globalRules>
</rewrite>
Also you can add URL rewrite rule using GUI. Before you start make sure that both of the direct HTTP-requests to brandview.auditedmedia.com and https://auditedmedia.com/data/media-intelligence-center/brand-view returns status 200 (OK).
Open IIS Manager by prompting inetmgr command.
Select brandview web site in the Connections pane and open URL Rewriting feature.
Create new empty rule using Add rule(s)... action.
Within the Edit Inbound Rule dialog, enter the following:
Name: Redirect to auditedmedia.com;
Requested URL: Matches the Pattern;
Using: Regular expressions;
Pattern: .*;
Action type: Redirect;
Redirect URL: https://auditedmedia.com/data/media-intelligence-center/brand-view.
Append query string: false;
Redirect type: See Other (303);
Save the rule by clicking Apply on the Actions pane.
Make sure that request to brandview.auditedmedia.com will be redirected to https://auditedmedia.com/data/media-intelligence-center/brand-view.
Repeat steps 1, 2 and select Redirect to auditedmedia.com rule.
Execute action Add condition(s)... with following:
Condition input: {QUERY_STRING};
Check if input string: Does Not Matches the Pattern;
Pattern: .+;
Ignore case: true.
Push OK button.
As result, all requests to brandview.auditedmedia.com with empty query string will be redirected to https://auditedmedia.com/data/media-intelligence-center/brand-view page. After testing you can change redirect type to 301.
After trying different combinations I finally got the redirect to work correctly and to be honest, I'm not sure how. Here is my config
<rewrite>
<rules>
<rule name="Brandview Redirect" enabled="true" stopProcessing="true">
<match url="^$" ignoreCase="true" negate="false" />
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" appendQueryString="false" redirectType="Temporary" />
<conditions>
<add input="{HTTP_HOST}" pattern="brandviewdev\.auditedmedia\.com" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
I have a rewrite pattern that I can't get to work.
if someone goes to my site by using this url:
http://example.com/article/page/1?_escaped_fragment_=
I would like my rewrite pattern to match the querystring and then rewrite my path to
/snapshots/snapshot_article_page_1.html
I tried writing a rule like this:
<rule name="Seo rewrite rule" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)_escaped_fragment_=(.*)" />
</conditions>
<action type="Rewrite" url="/assets/snapshots/snapshot__{REQUEST_URI}.html" appendQueryString="false" />
</rule>
But I just keep getting a 404 error (even though the page exists!). I even tried actually putting in the full URL to the page and it doesn't work, but if I put the URL in the browser, it shows it fine.
It appears I have done it.
The rule to match my example would be this:
<rule name="Seo rewrite rule" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)_escaped_fragment_=(.*)" />
</conditions>
<action type="Rewrite" url="/assets/snapshots/snapshot__{R:0}.html" appendQueryString="false" />
</rule>