3

I have pictures on a path looking like this:

/0/1/2/3/4/01234/screenshots/1.jpg

The URL to access it looks like this:

/static/0/1/2/3/4/01234/screenshots/1.jpg

I'd like it to look like this:

/0/1/2/3/4/01234/a-desc-of-the-picture/screenshots/1.jpg

Or something similar. The goal is to have the keywords in the URL for SEO.

But would like to tell nginx to serve

/0/1/2/3/4/01234/screenshots/1.jpg

When he sees:

/0/1/2/3/4/01234/a-desc-of-the-picture/screenshots/1.jpg

I don't want it to redirect the user to the proper URL, I just want it to do the mapping internally.

Is it possible ? How can I achieve this ?

I've seen something similar here, but I can't find a way to apply it to my case. Something noob friendly would be much appreciated.

e-satis
  • 409
  • 5
  • 17

1 Answers1

5

Assuming you've got a base path that's regexable, you can do something like:

location ~ ^/static/(././././.....)/[^/]+/(.*)$ {
    alias /location/on/filesystem/$1/$2;
}

Nginx's alias directive is more flexible than Apache's equivalent.

womble
  • 95,029
  • 29
  • 173
  • 228
  • There is typo. Exclamation mark "!" in location statement should be replaced with tilda "~". – AlexD Jul 18 '11 at 10:47
  • Thanks for that. You can edit directly though, rather than dropping in a comment. – womble Jul 18 '11 at 10:48
  • I can't. It prevents me from doing edits wich less than 6 characters long and I don't want to mess with other parts to overcome this limit. – AlexD Jul 18 '11 at 10:50
  • How odd... I never realised there was a "minimum edit limit". Oh well, thanks for letting me know anyway. – womble Jul 18 '11 at 10:59
  • Cool thanks. Can you show me how to do that with lighttpd too ? I changed the title. – e-satis Jul 18 '11 at 11:37
  • One question per question. Make a separate one for lighttpd. – womble Jul 18 '11 at 11:40
  • Fair enought :-) http://serverfault.com/questions/291394/how-to-make-lighttpd-remap-a-part-of-the-url-without-sending-a-redirection – e-satis Jul 18 '11 at 12:10