How to convert a apache url rewrite rule to IIS rule? - regex

I have url rewrite rule on my .htaccess file like this.
RewriteEngine on
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]
I need to use the same rule on my IIS .
I have used ^(.*)\.[\d]{10}\.(css|js) for the pattern.
I am not sure what to mention Rewrite URL.
I want to rewrite the url /css/structure.1234.css as /css/structure.css
Anyone knows please help?

I read your other ColdFusion question (regarding using regex to add a numerical value to a filename.)
We use an underscore and a 14 digit datestamp. Here's the IIS Rewrite Rule that we use. You can modify it for your needs.
<rule name="CSSJSDatestamp" stopProcessing="true">
<match url="^(.*)(_[0-9]{14}\.)(css|js)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{SCRIPT_NAME}" pattern="/_scripts/" />
</conditions>
<action type="Rewrite" url="{R:1}.{R:3}" />
</rule>
BTW, I don't use this method anymore with my ColdFusion-drive websites since installing IISpeed (PageSpeed for IIS). It automatically minifies, concats and caches JS/CSS files when the files are changed (without the need for cach-busting). It can also optimize images based on device support (ie, reduce size, auto-conversion to WebP, etc). I also really like that it can move all CSS & JSS to the HEAD of the HTML file, prioritize CSS and lazy loads all images "below the fold" with having to add any ColdFusion code or use jQuery.
For more info on IISpeed/Pagespeed, check out:
http://www.iispeed.com/
http://www.iispeed.com/pagespeed/insights
https://developers.google.com/speed/pagespeed/

Related

UrlRewriting.net rewrite .htm to extensionless, but ignore umbraco directories

I need to redirect all of the legacy .htm urls for my website to extensionless urls, so:
http://www.website.com/page.htm --> http://www.website.com/page
I created the following rule in config/UrlRewriting.config, which fixed the above issue, but created an issue where all of the backoffice paths are returning 404:
<add name="HtmToExtensionless"
redirect="Domain"
ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="http://www.website.com/(.*)(\.htm)"
redirectMode="Permanent"
destinationUrl="http://www.website.com/$1" />
So requests like these are returning 404:
http://www.website.com/umbraco/views/components/application/umb-navigationl?umb__rnd=7.4.2.1536555776
I'm pretty sure my problem is with my regex, but I'm not sure. Anyone care to advise?
Turns out I was being a dufus. Umbraco actually requests resources that have .html extensions, and my regex was grabbing those and stripping the .htm from them. Just had to check for the beginning and end of the string, and now we're golden:
<add name="HtmToExtensionless"
redirect="Domain"
ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="^http://www.website.com/(.*)(\.htm)$"
redirectMode="Permanent"
destinationUrl="http://www.website.com/$1" />

IIS RewriteUrl RegularExpression

I'm using IIS 7.5 and UrlRewrite Module 2. I've read the official documentation for the module here, as well as many examples on SO and tested my regex on Regexpal. Somehow the translation into the Web.config file is causing me problems. I've tried escaping the parantheses also.
The regex I'm using is:
^partners/(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)
a few example urls:
partners/var/emea
partners/msp/apac/
partners/var/na/abcdabcdabcd
Here is the web.config I have so far:
<rules>
<rule name="partnersListing" enabled="true" stopProcessing="true">
<match url="^partners/(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)" />
<action type="Rewrite" url="partnersList.aspx?type=${R:1}&region={R:2}" />
</rule>
</rules>
Not sure if I want the action to be Rewrite or Redirect yet, but the pattern isn't working either way. Any help is greatly appreciated
Per your answer to my question in the comments:
Is this rule in the site root web.config or a subdirectory web.config?
#Kev in a subdirectory: "/partners"
From the docs:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference?amp;clcid=0x409
Quote:
Note that the input URL string passed to a distributed rule is always
relative to the location of the Web.config file where the rule is
defined. For example, if a request is made for
http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a
rewrite rule is defined in the /content directory, then the rule gets
this URL string default.aspx as an input.
Based on this you need to change your url match to:
^(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)
Assuming your re-write rule exists in:
http://example.com/partners/web.config
There is just one other thing which is what appears to be an extraneous $ character in your redirect/rewrite url in front of back reference {R:1}, I'm not sure if you intended that to be there or not.

Need IIS 7 Url Rewrite Version 1.1 Regex Help To Remove Folder From Url

I am using:
IIS 7
Url Rewrite Module Version 1.1
Asp.Net MVC 4
I am hosting my sites in a shared hosting environment.
No site is hosted in the root folder as all sites are in their own physical folder and I use the domain name of the site as the physical folder name.
I would like to change my urls from this
http://www.mysite1.com/mysite1
http://mysite1.com/mysite1
to this
http://www.mysite1.com/
http://mysite1.com/
I have this and it doesn't work.
**DOES NOT WORK**
<rule name="RemoveFolder" stopProcessing="true">
<match url="mysite1/(.*)" />
<action type="Rewrite" url="/{R:0}" appendQueryString="true" />
</rule>
Can someone give me a regular expression that would add www to any urls and remove the folder that is showing up in the url?
OK. What I typed below, doesn't work for me. I thought it did, but I realized it didn't.
#Cheesemacfly, I'm trying to remove the subfolder from the url,
Redirect the user back to the site with the url MINUS the subfolder,
Make sure that there is NOT a continuous loop.
Cheesemacfly, your answer didn't work, but it did put me on the path to the full correct answer.
Thank you chesse for putting me on the right path.
This rule removes the subfolder from the url and redirects the user back to the page without the subfolder. The condition makes sure that I don't have a continual redirect. Once the url has been rewritten, that conditional will stop the url from matching again.
I placed this rule in the ROOT web.config of my hosting account.
The root ONLY contains a web.config and all of the subfolders.
<rule name="Cleanup" stopProcessing="true">
<match url="theFolderINeedToRemove/(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mywebsite.com/theFolderINeedToRemove$" />
</conditions>
<action type="Redirect" url="/{R:1}" appendQueryString="true" />
</rule>

IIS 7.0 rewrite module for file name

I have a website with many links to *.index.html pages that do not exist any more, all default documents have now changed to index.htm, I'm trying to set a URL redirect/rewrite to the new pages but with no luck, here is an exmaple:
http://example.com/somedirectory/maybeanotherdirectory/index.html
I need to redirect that request to:
http://example.com/somedirectory/maybeanotherdirectory/index.htm
I have added different rules to the web.config but so far I can only redirect to:
http://example.com/index.htm
How do I sustain the exact path while only changing index.html to index.htm?
Such an old question, and I stumbled across it purely by happenstance. In case anybody comes across it in look for an answer, you can install the IIS URL ReWrite module, and add the following to your web.config:
<rewrite>
<rules>
<rule name="Html to htm">
<match url="(.*)\.html" />
<action type="Rewrite" url="{R:1}.htm" />
</rule>
</rules>
</rewrite>
Cheers!

Help in migrating from ISAPI_Rewrite to IIS7 URL Rewriting

I've this ISAPI_Rewrite rule:
RewriteRule /([^/.?]+) /sc.asp?p=$1 [I,L]
This rule should rewrite Urls like:
/some-article
/article2
and shouldn't rewrite Urls like:
/home.asp
/admin (because there's a directory named admin)
/admin/
/sites/gutterman
It works great in ISAPI_Rewrite
Yesterday I bought a Windows 2008 R2 VPS, and I started to transfer my ASP-classic web application to the new server. Now, everything is working great, except for URL Rewriting. I google-ed a little bit and found that the IIS7 URL Rewrite module is completely different from the ISAPI_Rewrite.
I created a Blank Rule. In the pattern I set /([^/.?]+), in Action -> Action Type I set Rewrite and in Action -> Action properties -> Rewrite Url I set sc.asp?p={R:0}. It didn't work.
After some googling I found that I have to remote the / character in the start of my pattern (so my pattern is now ([^/.?]+)). So I did it, and now, it is just rewriting any url, and it should rewrite only urls that aren't directories or files.
Any ideas?
Thank you.
The simplest way to avoid rewriting URLs that actually point to existing files is to add these conditions:
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
Also, your match pattern shows a common misconception. The querystring is not passed to the Match URL pattern, so you don't need to look for ?.