Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url Rewriting for images

I'm trying to figure out how to enable short URL's for images on the website using Application_BeginRequest.

Currently, to open an image I have to use full URL like this:

http://mywebsite.com/ViewImage.aspx?album=123&id=456

I want the images to be accessible using short URLs:

http://mywebsite.com/123/456

ViewImage.aspx retrieves images dynamically from the database.

Assuming I should be using RewritePath method. But how exactly?

like image 990
SharpAffair Avatar asked Dec 06 '25 02:12

SharpAffair


2 Answers

You can accomplish this with URL rewrite http://www.iis.net/downloads/microsoft/url-rewrite You create rules in the web.config to map urls to files.

We use this image rule for example, to map version url's to real images. You can create something simulair for your urls. In IIS you can test you rules when you install the module

    <rule name="rewriteImgRule" stopProcessing="true">
      <match url="^v[0-9\.]+/img/(.*)$" />
      <action type="Rewrite" url="/img/{R:1}" />
    </rule>
like image 118
Ivo Avatar answered Dec 08 '25 15:12

Ivo


@Ivo's answer may well be the simplest but I personally prefer to avoid installing additional modules.

If you're using IIS7 (Windows 7/Server 2008 or later) you can use request handlers (.ashx files) then map various Urls to them...

See http://www.dotnetperls.com/ashx for a tutorial on setting one up.

Once you've got one working, you can examine the Request object to determine what Url was used exactly and extract the parameters any way you like

like image 41
Basic Avatar answered Dec 08 '25 17:12

Basic