10

We are having more than 300 pages in my website project. Over the time we have created a new server which is secure. This server is specially used for all the images in website.

So here is the scenario:

  • current implementation for images ( in aspx, in css )

    http://www.mysite.com/assets/common/image1.jpg
    
  • sometimes in webpage and css it is specified like this

    ~/assets/common/image1.jpg        
    
  • would like to use somethig like this.

    http://www.static.mysite.com/common/image1.jpg
    
  • and for secure pages

    https://www.static.mysite.com/common/image1.jpg
    

So as you can see all the images are coming from ~/assets folder BUT now I want to create a rule replacing ~/assets with http://static.mysite.com

How can I achieve this in IIS using rewrite rule.

EXAMPLE:

ASPX

 <img src="/assets/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />

<img src="http://mysite.com/assets/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />

Would like to have IIS rule, when finds above code, replace it with http://static.mysite.com/common/image1.jpg

 <img src="http://static.mysite.com/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />


<img src="http://static.mysite.com/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />
patel.milanb
  • 5,822
  • 15
  • 56
  • 92

3 Answers3

12

You need to create Outbound Rule in IIS. Rule will need following:

  1. Precondition should only check html files (I used default IsHTML)
  2. In "Matching the content with" choose elements in which you would like to check links
  3. Pattern is ^(.*)/assets/(.*)
  4. Action properties is http://static.mysite.com/{R:2}. R:2 reffers to second () in above regular expression. You could check what you need after click of "Test pattern" button.

Bellow simple rule which meets above:

enter image description here

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • How to configure this using `web.Config`, I want all the `Images` folder , `css` and `js` inside Content folder to point to azure cdn ? – Shaiju T Mar 06 '17 at 16:01
  • @stom all above changes are visible in web.config so you can copy-paste them – Piotr Stapp Mar 07 '17 at 12:12
6

You can try this

<rule name="assets redirection" stopProcessing="false">
    <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
    <action type="Redirect" url="{R:1}/{R:3}" />
</rule>

It will redirect whatever/assets/common/image1.jpg to whatever/common/image1.jpg

Update:

<rule name="assets redirection" stopProcessing="false">
    <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
    <action type="Redirect" url="static.mysite.com/{R:3}" />
</rule>
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • i would like it to redirect to http://static.mysite.com/common/image1.jpg by replacing http://mysite.com/assets/ – patel.milanb Jun 26 '13 at 08:56
  • I am using godaddy for hosting. So should I drop the above rewrite in the individual website virtual directory/folder or at the root? – Ganesh S Feb 17 '16 at 10:21
  • 1
    @SnehaJavalkar, Depends on which level you want to apply the rule. you can define it at root level also – Satpal Feb 17 '16 at 10:27
2

umm some how you can do like this

string imageUrl= Request.Url.Scheme + "://" + Request.Url.Authority + "/Image/logo/Logo.png";

Scheme is your web Schemes http or https etc. Authority is your web domain name with port if any. Then here it goes your image url completely.

I have used this logo url for the ssrs report service and got fine.Hope it ll work for you.

Comments and queries are welcome . Thank you.

Binod
  • 457
  • 4
  • 12