url rewriting of IIS - regex

with some hit and trail, i managed to remove the extention of the pages which i view for my website, now i want to replace all the ? and = and the & signs with backslash /, I am really Lost now, how do i write that rule in my web config
My Web.Config File:
<rewrite>
<rules>
<rule name="Redirect .cfm extension" stopProcessing="false">
<match url="^(.*).cfm$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).cfm$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .cfm extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.cfm" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.cfm" />
</rule>
</rules>
</rewrite>
with the above, i managed to get a page like this
http://website.ca/graphics?mode=Graphics%20Design%20%C2%BB%20Business%20cards&catID=35&section=graphics
the graphics? is the one which was before graphics.cfm?

Related

URL Rewrite iis server

I want to rewrite a url to index.html. the web.config below does the job until first path.
eg:- test.com/test1 to test.com/index.html but it doesnt work with multiple paths. eg:- test.com/test1/test2
my current web.config
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm not sure what I'm missing here. Please if any of you know, write the correct web.config.
Thank you.
You can try this rule:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/([^/]+)" />
</conditions>
<action type="Redirect" url="http://test.com/{C:1}/index.com" redirectType="Found" />
</rule>

IIS: Regex Including Dot/Period

I have the following rules. The second works but the first doesn't seem to to fire. For example, URL in question for the first rule is index.cfm?section=artists.az&id=22707 and for the 2nd rule which works, the URL is index.cfm?section=artists&id=22707
<rule name="Artists: AZ" stopProcessing="true">
<match url="^index\.cfm$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^section=artists\.az&id=([^=&]+)$" />
</conditions>
<action type="Rewrite" url="artist.php?id={C:1}" appendQueryString="false" />
</rule>
<rule name="Artists: index.cfm" stopProcessing="true">
<match url="^index\.cfm$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^section=artists&id=([^=&]+)$" />
</conditions>
<action type="Rewrite" url="artist.php?id={C:1}" appendQueryString="false" />
</rule>
ANy ideas?

Web.config regex get url sections

I'm trying to create a regex rule to use on Mautic's web.config since none was provided and i couldn't find any working option.
I got an example from a website which lists this option:
^([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)$
Which works fine with 2 blocks URLs like:
http://example.com/s/dashboard
but i also need to work with 3 and 4 blocks URLs like:
http://example.com/s/pages/edit/1?_=1457451067112&mauticUserLastActive=1&mauticLastNotificationId=2
I have managed to create a partial working regex:
(\/[a-z0-9_-]+)
But it won't work on web.config, it gives me a 404, what could be the problem ? As you can see i have a "working" example on RegExr
I did it like this.
<rules>
<rule name="Rewrite to index.php">
<match url="^([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)$" />
<conditions>
<add input="{URL}" negate="true" pattern="index.php(.*)" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}/{R:2}" />
</rule>
<rule name="Rewrite to index.php 2">
<match url="^([_0-9a-zA-Z-]+)?(\/[_0-9a-zA-Z-]+)?$" ></match>
<conditions>
<add input="{URL}" negate="true" pattern="index.php(.*)" ></add>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" ></add>
</conditions>
<action type="Rewrite" url="index.php/{R:1}/{R:2}" ></action>
</rule>
<rule name="Rewrite to index.php 3">
<match url="^([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)$" ></match>
<conditions>
<add input="{URL}" negate="true" pattern="index.php(.*)" ></add>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" ></add>
</conditions>
<action type="Rewrite" url="index.php/{R:1}/{R:2}/{R:3}" ></action>
</rule>
<rule name="Rewrite to index.php 4">
<match url="^([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)$" ></match>
<conditions>
<add input="{URL}" negate="true" pattern="index.php(.*)" ></add>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" ></add>
</conditions>
<action type="Rewrite" url="index.php/{R:1}/{R:2}/{R:3}/{R:4}" ></action>
</rule>
<rule name="Rewrite to index.php 5">
<match url="^([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)$" ></match>
<conditions>
<add input="{URL}" negate="true" pattern="index.php(.*)" ></add>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" ></add>
</conditions>
<action type="Rewrite" url="index.php/{R:1}/{R:2}/{R:3}/{R:4}/{R:5}" ></action>
</rule>
</rules>
Hope it helps

RegExp web.config - Select path after / and rewrite as URL param

I have a requirement to update the web.config file so that it replaces the path after the / and rewrites the URL as follows:
domain.com/acct01 -> domain.com/#/?id=acct01
domain.com/acct02 -> domain.com/#/?id=acct02
domain.com/acct03 -> domain.com/#/?id=acct03
...
Currently, for every account I am adding the following to the web.confg file. So for the above example I would need to do the following:
<rule name="acct01" stopProcessing="true">
<match url="^acct01$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/#/?id=acct01" />
</rule>
<rule name="acct02" stopProcessing="true">
<match url="^acct02$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/#/?id=acct02" />
</rule>
<rule name="acct03" stopProcessing="true">
<match url="^acct03$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/#/?id=acct03" />
</rule>
As you can see, this can become unwieldy after many accounts. I'm looking for a general way of capturing the value of after the last / inside of the URL and a more generalized way of replacing the URL in the action section after the id URL param. For example:
<rule name="addIDFromPath" stopProcessing="true">
<match url="[REGEX FOR GENERAL EXTRACTION OF PATH AFTER LAST /]" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/#/?id=[VALUE OF PATH EXTRACTED]" />
</rule>
If the URL contains a #, it should be ignored and left alone since this is an indication that it has been rewritten.
We are on a Windows Box with IIS installed so an alternative to the web.config file may be considered.
Thank you!
Not sure if I understood question correctly, but if the idea is to replace last / with /#/?id= then this pattern will do the work, replace
(?=\/([a-z0-9]+)$)
with /#/?id=.
You can test it here: https://regex101.com/r/kW8zU0/1
EDIT
Probably something like this:
<rule name="addIDFromPath" stopProcessing="true">
<match url="(^|\/)([a-z0-9]+)(?=$)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/#/?id={R:2}" />
</rule>
Not sure about group number, could be {R:1}
Pattern: (^|\/)([a-z0-9]+)(?=$)
Demo: https://regex101.com/r/kW8zU0/4

IIS7 URL Rerwrite - how to replace all underscores with hyphens in a Regex?

I am using the URL Rewrite feature in IIS7 to turn the URL:
/main.asp?category=Name_Of_A_Product
Into:
/category/name-of-a-product/
I have created the redirect & rewrite rules below which do the majority of the work, except I cannot find a way of replacing the underscores with hyphens.
Each URL can have between zero and many underscores and I'm trying to replace them in a single regular expression, to avoid chains of 301 redirects (as I believe that is bad for SEO).
Do you know how (or if) this is can be done?
<rule name="Redirect REAL to FRIEDNLY" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([^=&]+)($|&(.*))$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>
<rule name="Rewrite FRIEDNLY to REAL" stopProcessing="false">
<match url="^category/([^/]+)/?$" />
<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="main.asp?category={R:1}" />
</rule>
Unfortunately IIS7 has a few limitations:
you can only capture 9 groups C:1 ... C:9
there is only one string function and that's ToLower
Because of that you'll be limited to a URL with a maximum of 9 words separated by max 8 underscores (eg. /main.asp?category=One_Two_Three_Four_Five_Six_Seven_Eight_Nine) and you'll be forced to use 9 rewrite rules:
Single Word: /main.asp?category=Product
<rule name="Redirect REAL to FRIEDNLY 1" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>
Two Words: /main.asp?category=Some_Product
<rule name="Redirect REAL to FRIEDNLY 2" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}/" appendQueryString="false" />
</rule>
Three Words: /main.asp?category=Some_New_Product
<rule name="Redirect REAL to FRIEDNLY 3" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)_([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}-{ToLower:{C:3}}/" appendQueryString="false" />
</rule>
            ...            ...            ...            ...            ...            ...