Can I have a filter (or match) only the below mentioned 2 urls by using Regular expression?
http://www.domain.com/owner/Marketing and http://www.domain.com/owner/getinfo )?
UPDATE Usage is as below.
<rule name="Skip HTTPS" enabled="true" stopProcessing="true">
<match url="I need regex here" ignoreCase="true" />
<conditions>
<add input="{HTTP}" pattern="OFF" />
</conditions>
<action type="None" />
</rule>
UPDATE 2:
If I put this way,Will it work?
<match url="(Marketing|getinfo)" ignoreCase="true" />
If i understand correctly what you want, then it is strait forward: (http:\/\/www\.domain\.com\/owner\/Marketing | http:\/\/www\.domain\.com\/owner\/getinfo). see http://regex101.com/r/yW3aP9
You can use the pipe | character for alternation and parenthesis. Add a leading ^ and trailing $ "binds" the expression to only match the exact URLs with no leading or trailing garbage and escape the slashes (which acts a RegEx delimiters) and dots (. which match any character). So:
/^http:\/\/www\.domain\.com\/owner\/(Marketing|getinfo)$/
ok this one seems to work,
/http:\/\/www\.domain\.com\/owner\/(Marketing|getinfo)/
but regexpal is not matching the strings up when you compare the entire string with a carett and dollar sign ^$, maybe a more experienced person will know why
Related
I am writing a rewrite rule in web.config file and want to match against a url (using regular expression) if it containes:
*/admin*
So as long as the url contains above it should match. Example of legal matches:
http://test.com/admin
https://test.com/admin
http://test.com/admin/
http://test.com/admin/test
http://test.com/admin/grgr/hht/
Example of illegal matches:
http://test.com
https://test.com/adminpage
https://test.com/adminpage/
I have tried the followings without success:
<match url="(.*)/admin$" ignoreCase="false" />
<match url="/admin?" ignoreCase="false" />
<match url=".*/admin?" ignoreCase="false" />
Try this regex
.*admin(\/.*|$)
Try something like this
.*\b\/admin\b.*
https://regex101.com/r/zq0SAy/1
Details:
\b asserts position at a word boundary
I'm trying to make a IIS redirect rule to redirect from this url pattern, but it beats me:
https://www.mycompanyPLC.com/en/lorem/ipsum/whatever
to
https://www.mycompanyLTD.com/lorem/ipsum/whatever
Basically I need to replace PLC with LTD and if there is the "/en/" group in url, this has to be removed.
You can achieve your both the requirements using the single regex provided /en/ is preceded by .com. Something like:
(.*?)PLC\.com(?:\/\ben\b)?(.*)
Explanation of the above regex:
(.*?) - Represents 1st capturing group capturing everything before PLC lazily.
PLC\.com - Matches PLC.com literally.
(?:\/\ben\b)? - Represents a non-capturing group matching \en literally zero or one time. \b represents a word boundary.
(.*) - Represents the second capturing group matching everything after \en greedily.
$1LTD.com$2 - For the replacement(or redirection in this case) part you can get away with this string where $1 represents the first captured group and $2 represents the second captured group. In your case; you can use {R:1}LTD.com{R:2}.
You can find the demo of the above regex in here.
Please refer to below URL rule.
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="en(.*)" />
<action type="Redirect" url="https://www.mycompanyLTD.com{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
There is no need to match a /en URL fragment forcibly. We redirect the request as long as we found that we have a /en URL segment. so does the http/https URL segment.
Feel free to let me know if there is anything I can help with.
After several hours of lecturing Regex I've created this rule and seems to be working (I've tested several scenarios):
^(http|https)://?(www.)mycompanyPLC.com/en?(.*)
and the Redirect URL from IIS is:
https://www.mycompanyLTD.com/{R:3}
Later edit:
The rule in IIS is like this:
<rule name="Replace PLC with LTD and remove /en/" enabled="true" stopProcessing="true">
<match url="(.*?)PLC\.com(?:\/\ben\b)?(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}ltd.com{R:2}" />
</rule>
Test urls were this format:
http://webdev.myCompanyplc.com/en/our-experience/retail
{R:1} = http://webdev.myCompany
{R:2} = /our-experience/retail
Regex expression was ok, but redirect still didnt work
I have the following IIS rule which is supposed to redirect if the URI does not contain the word Api:
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^((?!Api).)*$" negate="false" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
This was working fine until I added a token as a query parameter for a route. Now when it tries to match that URI it will go out of memory.
How would I have to write the pattern so it looks only in the first 30 characters? The /Api/ route will never appear later. This way I will make sure that the regular expression matching does not run out of memory when a token is present.
To make sure Api does not occur within the first 30 chars you may use
pattern="^(?!.{0,27}Api).*"
Details
^ - start of string
(?!.{0,27}Api) - a negative looakahead that matches a location that is not immediately followed with any 0 to 27 chars (other than linebreak chars) and Api after them
.* - any 0+ chars (other than linebreak chars).
I am using a regex in a rewrite rule (on IIS 8) to deny access to all files and folders starting with a dot (.)
This way, these Urls cannot be accessed :
https://example.com/.forbidden_file
https://example.com/.forbidden_folder/somefile.txt
<rule name="Deny access to files and folders starting with . or /." patternSyntax="ECMAScript" enabled="true" stopProcessing="true">
<match url="(^\.|\/\.)" ignoreCase="true" negate="false" />
<action type="AbortRequest" />
</rule>
Today I would like to add an exclusion, in order to allow access to a specific file named ".specific_allowed_file", and a specific folder named ".specific_allowed_folder", like this :
https://example.com/.specific_allowed_file
https://example.com/.specific_allowed_folder/somefile.txt
I have managed to achieve it by adding two conditions, like this :
<rule name="Deny access to files and folders starting with . or /." patternSyntax="ECMAScript" enabled="true" stopProcessing="true">
<match url="(^\.|\/\.)" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{PATH_INFO}" pattern="(.*)(\.specific_allowed_file$" negate="true" />
<add input="{PATH_INFO}" pattern="(.*)(\.specific_allowed_folder)(.*)" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
Do you know if this is a reliable implementation, or if I should rather use another method, like modifying my matching regex ?
Thank you very much,
Your approach is a very good one since it allows users add conditions using readable and clear code. One thing should be noted: pattern="(.*)(\.specific_allowed_folder)(.*)" will also allow a .specific_allowed_folder_WITH_MORE_TEXT_HERE as well. You may fix it to match only folders by adding / into the first and third groups and making them optional by placing a ? quantifier right after the groups:
<add input="{PATH_INFO}" pattern="^(.*/)?(\.specific_allowed_folder)(/.*)?$" negate="true" />
If you want a single regex to do the whole job, use a regex with a negative lookahead:
<match url="(^|/)\.(?!(specific_allowed_file|specific_allowed_folder)(/|$))" ignoreCase="true" negate="false" />
See the regex demo.
Details
(^|/) - start of string or /
\. - a dot
(?!(specific_allowed_file|specific_allowed_folder)(/|$)) - that is not immediately followed with specific_allowed_file or (|) specific_allowed_folder substrings that are followed with / or end of string ($).
I'm using Mod Rewrite for IIS 7.0 from iis.net and want to redirect requests:
http://example.com/users/foo to http://example.com/User.aspx?name=foo
http://example.com/users/1 to http://example.com/User.aspx?id=1
I have created 2 rules:
<rule name="ID">
<match url="/users/([0-9])" />
<action type="Rewrite" url="/User.aspx?id={R:1}" />
</rule>
<rule name="Name">
<match url="/users/([a-z])" ignoreCase="true" />
<action type="Rewrite" url="/User.aspx?name={R:1}" />
</rule>
It passes a test into iis mmc test dialog, but doesn't in debug (URL like http://localhost:9080/example.com/users/1 or …/users/foo) and doesn't on real IIS!
What have I done wrong?
The obvious problem is that your current regexes only match one character in the user name or one number. You'll need to add a plus quantifier inside the parentheses in order to match multiple letters or numbers. See this page for more info about regex quantifiers. Note that you won't be matching plain URLs like "/users/" (no ID or name). Make sure this is what you intended.
The other problem you're running into is that IIS evaluates rewrite rules starting from the first character after the initial slash. So your rule to match /users/([0-9]) won't match anything because when the regex evaluation happens, the URL looks like users/foo not /users/foo. The solution is to use ^ (which is the regex character that means "start of string") at the start of the pattern instead of a slash. Like this:
<rule name="ID">
<match url="^users/([0-9]+)" />
<action type="Rewrite" url="/User.aspx?id={R:1}" />
</rule>
<rule name="Name">
<match url="^users/([a-z]+)" ignoreCase="true" />
<action type="Rewrite" url="/Users.aspx?name={R:1}" />
</rule>
Note that you're choosing Users.aspx for one of these URLs and User.aspx (no plural) for the other. Make sure this is what you intended.
BTW, the way I figured these things out was by using IIS Failed Request Tracing to troubleshoot rewrite rules. This made diagnosing this really easy. I was able to make a test request and look through the trace to find where each rewrite rule is being evaluated (it's in a section of the trace called "PATTERN_MATCH". For the particular PATTERN_MATCH for one of your rules, I saw this:
-PATTERN_MATCH
Pattern /users/([0-9]+?)
InputURL users/1
Negate false
Matched false
Note the lack of the beginning slash.
You should use <match url="/users/([0-9]+)" /> and <match url="/users/([a-z]+)" ignoreCase="true" />, respectively, to match the complete id/user and not just their first letter/digit. But I don't know why your regex would have failed on a single digit, so there must be another issue, too.
As for your second question, I'm not sure I understand completely. How can you tell the difference between a folder name and a user name? Will a folder always have a trailing slash?