regex to find vanity URLs from IIS rewrite rules - regex

I need to extract all the vanity URLs redirect rules but exclude the redirect rules.
<rule name="welcome2020" stopProcessing="true">
<match url="welcome2020" />
<action type="Redirect" url="https://www.mywebsite.org/Pages/.welcome2020aspx" appendQueryString="false" />
</rule>
<rule name="Page to Page Redirect">
<match url="/Staff/Pages/Ashley.aspx" />
<action type="Rewrite" url="services/Staff/Pages/Ashley.aspx" />
</rule>
i need to match all <match url="whatever" /> types that don't contain .aspx The only thing I have figured out is that I will need a negative lookahead. but not sure how to implement it.
I basically need something like this, but for my redirect rules.
https://www.regextester.com/15

You can just try:
^(?=.*match)(?!.*\.aspx).*
Please see the DEMO :)

Related

IIS Rewrite - redirect site to new domain capturing the language embedded in the URL

I am trying to write a regex to redirect the URL to a new domain. I wrote IIS Rewrite rule for this:
<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net(\/([a-zA-Z]{2,3}-[a-zA-Z]{2,3}|en)\/?)?(.*$)" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://my-new-domain.com/en-us/{C:4}" appendQueryString="true" />
</rule>
It works fine when the language is not added to the initial URL, however, some of the pages have the language added after the domain which results in double language appearance in the end URL.
So basically I would like to redirect things like:
my-www-en.sites.company.net/some-page/another/page/
www.my-www-en.sites.company.net/some-page/another/page/
my-www-en.sites.company.net/de-de/some-page/another/page/
www.my-www-en.sites.company.net/de-de/some-page/another/page/
my-www-en.sites.company.net/en/some-page/another/page/
to redirect to:
https://my-new-domain.com/en-us/some-page/another/page/
My current regex does not capture these groups correctly (even when it does while testing the regex in IIS rewrite) and I struggle to make it work. Right now everything gets redirected to the homepage instead to particular websites. Could you please help?
Please try this rule. The regular expressions can match all urls above.
<rule name="test">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net$" />
<add input="{REQUEST_URI}" pattern="(/.*)?(/some-page/another/page/)" />
</conditions>
<action type="Rewrite" url="https://my-new-domain.com/en-us{C:2}" />
You can change rewrite to redirect.

Redirect bookmarked URLS via IIS Rewrite Module is not working properly even though URL pattern matches in testing

I've read so many forums and I did possibly whatever I could. My outbound rule to works in terms of rewriting the URL for SEO purposes but my Redict URL which in case the changed URLs marked in our users' bookmarks does not work.
I am using IIS 10.0.
The URL that needs changing:
http://agmodel.com/files/content/insights/publishing/e_clouds.pdf
To:
http://agmodel.com/assets/content/insights/publishing/e_clouds.pdf
So only thing I am changing is the string "files" to "assets".
Here is what I've tried:
Attempt 1:
<rule name="Redirect" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(https?:\/\/[^\/]+)\/files\/(.*)" />
<add input="{REQUEST_URI}" pattern="^/assets" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/assets/{C:2}" appendQueryString="false" redirectType="Found" />
</rule>
I tried to make sure that the first pattern is always the domain the second pattern is files.
Attempt 2:
<rule name="assets-to-files" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/files\/(.*)" />
<action type="Redirect" url="{R:1}/assets/{C:1}" appendQueryString="false" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="\/files\/(.*)" />
</conditions>
</rule>
So whenever I test whether the bookmarked old URL will change to the new one, it does not work. It gets green light during pattern match testing in IIS 10.
What am I doing wrong here?
You may use a very simple rule here:
<rule name="assets-to-files">
<match url="^files/(.*)" />
<action type="Rewrite" url="assets/{R:1}" />
</rule>
The URL you want to match is http://agmodel.com/files/content/insights/publishing/e_clouds.pdf. The url attribute in match node will receive files/content/insights/publishing/e_clouds.pdf as input, so you want
^files/(.*)
It will match files/ at the start of the string and then will capture into {R:1} any 0 or more chars other than newline.
In the action node url attribute, all you need is to specify the assets/ new path and append what you captured into {R:1}.

IIS - Configuration file is not well-formed XML

<rule name="Posts2" enabled="true">
<match url="^index.cfm\?section=latest.news&id=(.*)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
Problem is with the regex - any idea at all? I can't seem to work it out. I've run it on http://www.xmlvalidation.com/ and it says
The reference to entity "id" must end with the ';' delimiter.
When I change the ampersand to & the rewrite doesn't seem to work on URLs such as /index.cfm?section=latest.news&id=14726 despite it passing the test in IIS. Even when I escape the dots and question mark it doesn't work
Try using a {QUERY_STRING} input using conditions perhaps:
<rule name="Posts2">
<match url="index\.cfm$" />
<conditions>
<add input="{QUERY_STRING}" pattern="section=(latest\.news)" />
<add input="#{C:1}#_{QUERY_STRING}" pattern="#([^#]+)#_.*id=(\d+)" />
</conditions>
<action type="rewrite" url="post.php?id={C:2}" appendQueryString="false"/>
</rule>
URL's containing query strings are usually better handled this way, although if you really prefer not going this route then make sure you have the all the proper tags included, and also try escaping the . in between latest\.news.
<rewrite>
<rules>
<rule name="Posts2">
<match url="^index\.cfm\?section=latest\.news&id=(\d+)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
</rules>
</rewrite>

Web.config regex OR oporator

I have the following rule in my webconfig
<rule name="accommidationRewrite2" enabled="true" stopProcessing="true">
<match url="accommodation/(?!results|!property).*?(.*)/" />
<action type="Rewrite" url="/index.cfm/?path=/accommodation/results/params/location/{R:1}/" />
</rule>
I'm trying to get the rule to kick in is the URL is not 'accommodation/results/....' OR 'accommodation/property/.....'
At the moment it only work for the results URL. Can I get it to work for both URL's?
Remove the ! symbol which was present before the property ,
accommodation/(?!results|property).*?(.*)/

URL Rewrite 2.0 Wildcards - IIS 7.5

<rule name="WomensSilverBangles" patternSyntax="ExactMatch">
<match url="/Bangles/Silver/Womens.aspx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="Collections/jewellery.aspx?AXSW_CategoryID=Bangles&AXSWFinenessId=Silver&AXSW_GenderID=Womens&MenuName=Jewellery&SiteMapNode=Silver Bangles&depth=2" />
</rule>
Can this be written using wildcards?
As you can see, the three variables in the match URL are used in the rewritten URL string.
I know it's been a while since this question was posted, but there is an excellent article on rewriting here and based on that, you could try experimenting with something like this(not tested):
<rule name="3levelcatchall" patternSyntax="Wildcard">
<match url="/*/*/*.aspx"/>
<action type="Rewrite" url="Collections/jewellery.aspx?AXSW_CategoryID={R:1}&AXSWFinenessId={R:2}&AXSW_GenderID={R:3}&MenuName=Jewellery&SiteMapNode={R:2} {R:1}&depth=2" appendQueryString="false"/>
</rule>