iis rewrite redirect all pages to homepage - regex

I'm attempting to redirect all pages on a IIS site to the homepage/root. My rule below seems to work however I get a "localhost redirected you too many times" error.
The rule needs to exclude anything before the slash e.g mysite.com/mypage/
<rule name="redirect_all_bar_home_root" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="^$" negate="true"/>
</conditions>
<action type="Redirect" url="http://localhost" />
</rule>

This rule will do that:
<rule name="redirect_all_bar_home_root" stopProcessing="true">
<match url="^$" negate="true"/>
<action type="Redirect" url="/" />
</rule>
Regex: ^$ is matching when string is empty (it is homepage)
negate="true" is negating regexp. All URLs which are not homepage

I've just had a requirement to do this. The following rule will do what you need but with the exception that it will not attempt to redirect file urls so images still work on your homepage:
<rule name="RedirectAllToRoot" stopProcessing="true">
<match url="^$" negate="true" />
<action type="Redirect" url="/" redirectType="Temporary" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
</rule>
redirectType="Temporary" gives a 307 redirect status code

Related

IIS URL Rewrite for Laravel Application Not working for multiple URI Segments

I am having a scenario that if URL doesn't contain anything in URI segment then no need to rewrite to index.php but if contains then rewrite it.
For Example: http://www.example.com/ or http://www.example.com No Rewrite Rule Required
But if URL is http://www.example.com/homepage or http://www.example.com/user/1 or http://www.example.com/edit-user/123/456
then it should be rewrite to index.php
I tried below.
<rules>
<rule name="custom rule 1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<action type="Rewrite" url="index.php" />
</rule>
It is working for http://www.example.com/ and http://www.example.com/homepage but showing 404 for others pages.
The URI segments are dynamic and it can be multilingual.
Technologies used IIS, PHP (Laravel)
You can try this, it will match all kinds of URIs whether dynamic or multilingual.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions>
<add input="{REQUEST_URI}" pattern=".*" />
</conditions>
</rule>
Test result
Fail request tracing
If this rule is only used for some URIs like user or edit-user, maybe this rule is more suitable.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="user/(.*)" />
<add input="{REQUEST_URI}" pattern="edit-user/(.*)" />
</conditions>
</rule>

Building IIS Redirect Rule to Redirect Product Page to New URL Passing Product ID

I'm attempting to use the URL Rules engine in IIS 7 to redirect our current product pages to our new product pages.
Old Pages:
example.com/itemform.aspx?item=X12878
example.com/itemform.aspx?item=Y87304&showmenu=T
New URL
example.com/c/product/X12878
example.com/c/product/Y87304
Attempt
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([.a-zA-Z0-9]+)$" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
Complete Rewrite Rule w/ Suggestion by Emma
<rewrite>
<rules>
<rule name="Product Page Redirect" stopProcessing="true">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<conditions>
</conditions>
<action type="Redirect" url="https://newsite.com/x/product/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Accoridng to your description, if you want to use url rewrite to get the right query string.
More details, you could refer to below codes:
<rule name="QueryStringRue" enabled="true" stopProcessing="true">
<match url="itemform.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="^item=([0-9a-zA-Z]*)" />
</conditions>
<action type="Redirect" url="http://example.com/c/product/{C:1}" appendQueryString="false" />
</rule>
Result:
My guess is that maybe removing the end $ anchor might simply make the match valid:
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
but, maybe not.

Rule not working in rewrite rules despite valid regex

Our webconfig file uses Url Rewrite, essentially pushing any http traffic to https
This works fine other than developing locally. For a while we have to simply remember to comment out the code from the web.config and uncomment it again for commit. Naturally this isn't a good way to work.
The code is simple
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}"/>
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
And as per the regex101 , it works!
https://regex101.com/r/3Mz6w1/1
However, when on localhost, I am still directed to HTTPS
Why does it work in regex101 and not in my web.config file
This seems to be related to Redirect rule not working
quote from URL Rewrite Module Configuration Reference
A rewrite rule pattern is used to specify a pattern to which the current URL path is compared.
...
A pattern is specified within a <match> element of a rewrite rule.
According to this piece of official information, you must be sure that <match url compares only with URL paths which never contain host names, not the entire URL.
For Url Rewrite Module, URL path of this question is questions/44944175/rule-not-working-in-rewrite-rules-despite-valid-regex for example. No stackoverflow.com no https:// no query strings but only the path without leading slash.
To ignore requests for host names containing local you need some conditions looking for a local match with HTTP_HOST header.
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW" stopProcessing="true">
<match url=".*" />
<conditions>
<!-- continue if http host name does not contain "local" -->
<add input="{HTTP_HOST}" pattern="local" negate="true" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost" stopProcessing="true">
<match url=".*" />
<conditions>
<!-- continue if http host name does not contain "local" -->
<add input="{HTTP_HOST}" pattern="local" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>

IIS redirect with regex [duplicate]

Whenever someone makes request over HTTP protocol I rewrite the url to make it HTTPS. This is the code in web.config:
<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
<match url="^(?!https://).*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="https://abc.com/{R:1}" />
</rule>
However when I browse on http:// I get IIS error
HTTP Error 500.50 - URL Rewrite Module Error. The expression "https://abc.com/{R:1}" cannot be expanded.
How can I resolve this? I am utterly confused.
The matches are zero based.
<action type="Rewrite" url="https://abc.com/{R:1}" />
Won't work because you only have one match. You need:
<action type="Rewrite" url="https://abc.com/{R:0}" />
Also, this won't work, because you can only match on the path below the site root.
<match url="^(?!https://).*" ignoreCase="false" />
It looks like you are checking for ssl. Try this instead:
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
You can redirect through web config to
Hope it will help full
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^abc.com$" />
</conditions>
<action type="Redirect" url="http://www.abc.com/{R:0}" redirectType="Permanent" />
</rule>

IIS 7 Redirect using regular expression

We currently have a wordpress blog running under a sub domain http:// blog.domain.com. That site is also currently running via reverse proxy as http:// www.domain.com/blog that points to the original subdomain site.
Currently both sites are running correctly but the issue we have is that we want the sub domain site to redirect to the reversed proxy site and not render. We only want the site to render as http:// www.domain.com/blog. I have been attempting to use the URL Rewrite in IIS 7 on a server 2008 machine.
I think the issue I am having is with the regular expression. I tried using -- ^(blog.)* -- and when I test it in IIS it returns that it matches (blog.domainname.com) but the site itself does not redirect when I open it in a browser. I'm not sure what I am missing. Thanks in advance for your help.
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" /></rule>
<rule name="wordpress" patternSyntax="Wildcard">
<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" /> </rule>
<rule name="redirect to /blog" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(blog.)*" />
<action type="Redirect" url="http:// www.domainname.com/blog" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)" />
</conditions>
</rule>
</rules>
</rewrite>
The problem is probably that your rewrite rules are not running when you hit that URL.
Make sure that it's actually mapped to that site.
Sorry, I just noticed that your rule is not enabled:
<rule name="redirect to /blog" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
You'd have to modify the enabled attribute to True.