I’ve been trying to get my head around nginx rewrites today and have read several questions/answers here as well as thoroughly read several guides I have come across.
While most things come to me quite naturally, i’m just not grasping it, perhaps due to my inexperience with any form of regular expressions.
I have a download.php script which overrides my nginx headers and forces the browser to perform a download with a ‘save as’ dialog, rather than open a fresh page.
It works perfectly however I would like to hide the url of the script itself from users.
To be specific, I would like to change this URL:
http://www.example.com/scripts/download.php?id=http://www.example.com/images/0/1/fullsize/96698298450ca01a60875e_34.jpg
So that to users it simply appears as the id GET parameter:
http://www.example.com/images/0/1/fullsize/96698298450ca01a60875e_34.jpg
But for the life of me I can’t seem to make it work.
Hopefully someone can come to rescue. Thanks in advance.
EDIT:
Just to add some further clarity to my question… The ideal solution would be for me to be able to have a link like this in my webapp:
http://www.example.com/images/0/1/fullsize/96698298450ca01a60875e_34.jpg?dl=1
Have nginx pick up that it has a dl=1 AND is in the /images/ directory and then rewrite it to use the download script:
http://www.example.com/scripts/download.php?id=http://www.example.com/images/0/1/fullsize/96698298450ca01a60875e_34.jpg
This way when images are only being viewed they are going through the script, and the imges are only rewritten and routed through the script when the dl=1 param is in place.
If I understand your question correctly, you want to place links like
http://www.example.com/images/0/1/fullsize/96698298450ca01a60875e_34.jpg&dl=1in your web app, and when the user clicks the link they get redirected to the download.php script?The rewrite directive below should accomplish that, assuming you are redirecting to the same host. This will be applied to any url path starting with
/images/and query parameterdl=1. This will do a 302 (temporary) redirect. If you want a permanent (301) redirect, replaceredirectwithpermanent.If you’re trying to go the other way (have links to the download.php script but redirect users to the actual resource url – not sure why, and probably a bad idea to do such a blind redirect) you could do it with this directive (it would be worth checking the
idquery string parameter is there)Let me know if either of these solutions is in the direction you want to go, and we can adjust it to fit your needs.