Urlrewrite for photo name - Web

Asked

Viewed 31 times

2

Today have the urls of my website images as:

/images/tb/1280077_894mvzfxoojqb.jpg

I would like to be able to rename this to the generated HTML

Apartamento_em_SaoPaulo.jpg or something similar.

Possible options.

Apartamento_em_SaoPaulo__1280077_894mvzfxoojqb.jpg

or

/1280077_894mvzfxoojqb/Apartamento_em_SaoPaulo.jpg

This is for SEO reasons. That is, No html he would be with a name but would physically fetch the photo with another name.

Something like:

RewriteRule  ^/imagens/([a-zA-Z0-9_-]+).jpg /imagens/$1.jpg

But it wouldn’t be feasible to physically re-name the photos (it has over 2 million images), it would have to be something like Route or UrlRewrite

  • It can be just like that? Fotos/1280077

  • Not for SEO reasons it was asked that the photo had a syntax of type casa_em_Campinas_2_dormitorios.jpg but this photo would be physically saved as 123142342423.jpg so it needs to be a Rewrite to do this. but I don’t know how

1 answer

2


Step 1: Configuring the Request Engine

First, you need to inform your application that it will treat file-terminated requests differently. To do this, modify your file web.config, adding the following:

<configuration>
  <system.webServer>
    <handlers>
      ...
      <add name="JpegFileHandler" path="*.jpg" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

Step 2: Setting the Route

In your file App_Start/RouteConfig.cs, configure a route like this:

routes.MapRoute(
    name: "Fotos",
    url: "{foto}.jpg",
    defaults: new { controller = "Fotos", action = "Obter", foto = UrlParameter.Optional }
);

Step 3: RouteExistingFiles

This prevents existing files from being returned with the original name. You can comment on this line later.

routes.RouteExistingFiles = true;

routes.MapRoute(
    name: "Fotos",
    url: "{foto}.jpg",
    defaults: new { controller = "Fotos", action = "Obter", foto = UrlParameter.Optional }
);

Step 4: Method of Controller

In FotosController, write the following method Obter:

public ActionResult Obter(string nomedoarquivo)
{
    // Execute aqui sua lógica para recuperar o arquivo.
    return File(variavelComAFotoComoByteArray, "image/jpeg");
}

variavelComAFotoComoByteArray needs to be a byte[].

  • I put it as right but I’ll only be able to test tomorrow, but I understand how it will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.