Forum Moderators: phranque

Message Too Old, No Replies

Using htaccess to redirect Downloads from a particular folder

         

ripejuice

10:24 pm on Oct 5, 2008 (gmt 0)

10+ Year Member



I am having a free download site and Can I use htaccess to redirect all downloads requested from a particular section(which is a folder in my server) to the another selected file of my choice.

If my site is www.mydomain.com and having a folder www.mydomain.com/downloads/2008/

I want to redirect all the download requests coming to this folder to a particular file

If user requests for any of these files
www.mydomain.com/downloads/2008/updates1.zip
www.mydomain.com/downloads/2008/updates2.zip
www.mydomain.com/downloads/2008/updates3.zip

then he should get this file
www.mydomain.com/downloads/2008/updates.zip

g1smd

10:36 pm on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes. Very easy.

You can find example code that is very close to what you require in the linked threads at the top of this forum.

Post your best effort code here as a basis for discussion.

ripejuice

10:59 pm on Oct 5, 2008 (gmt 0)

10+ Year Member



Can i Use this one

RewriteEngine On
RewriteRule .*\.(rar¦zip¦exe¦mp3)$ /updates.zip [L]

where should i put this file...is it in root or that folder.

g1smd

11:05 pm on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you put it in the root it would match every RAR, ZIP, EXE, and MP3 file on the entire site.

That is, the

.*
says "match everything", any path whatsoever.

To be safe, either:
- put that file inside the folder that it refers to, and use a slightly more specific pattern to match,
- or else use a very much more specific pattern than "everything" when you add it to the .htaccess file in the root folder.

Do you want the user to continue to see the URL they requested, or should it change to the new one?

With your scheme, as currently written, you have a rewrite and they continue to see the URL they requested.

That is, in your original post you asked for a "redirect", but the code you presented is for a "rewrite". They are slightly different things, and understanding the difference is crucial.

ripejuice

5:36 am on Oct 6, 2008 (gmt 0)

10+ Year Member



"Do you want the user to continue to see the URL they requested, or should it change to the new one? "
yes ...i want the user to see the old url itself.

But is there any option to RENAME the file which is given to the user to the name of the actually requested file.
That is if he is looking for "updates-october.zip" then as per the above redirect he will be seeing the file name as updates.zip after download.Is there an option to rename the file as per requested.?

ripejuice

6:14 am on Oct 6, 2008 (gmt 0)

10+ Year Member



the code is not working
RewriteEngine On
RewriteRule .*\.(rar¦zip¦exe¦mp3)$ /updates.zip [L]

getting a 404 not found error when i request for any files

g1smd

8:41 am on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your rewrite, you are calling for /updates.zip

Is the updates.zip file located in the root folder of the site?

Again, your code seemingly calls for any request for any RAR file, any ZIP file, any EXE file, and any MP3 file, located in any folder in any place anywhere on the entire website to always be rewritten to pick up this one ZIP file.

If the rewrite is only supposed to happen for those three explict examples, then your pattern needs to be a bit more specific than "match everything everywhere".

Another important consideration is which folder this .htaccess file going to be uploaded to. The answer to that dictates how the code should be written.

As for renaming, the user will see the name they requested exactly as they requested it in the URL. That's a usability issue: it is likely that users will download update1 and download update2 and download update3 and then be a bit puzzled why they have now got three identical files.

As coded at the moment, I could request yoursite.com/ork/ork/foo/bar/wibble.zip and still get a copy of that file. Even more dangerous, I could request yoursite.com/ork/ork/foo/bar/wibble.mp3 and I would be sent a ZIP file but it would save to my computer with "mp3" as the extension and it would fail to play.

ripejuice

3:35 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



sorry...didn't really understand what you said.....its my mistake

Actually this is my real site structure

www.mydomain.com/updates/

I release updates every month to my software and store it as updatejan2008.zip,updatefeb2008.zip etc in the folder
www.mydomain.com/updates/ so the link becomes www.mydomain.com/updates/updatefeb2008.zip or www.mydomain.com/updates/updatejan2008.zip

Due to my fault i accidently deleted the whole folder and i don't have a backup to restore them.So if the user requests for the old updates then they will get a dead link.

So what i want is that i want to redirect all those old updates that is updatejan2008.zip,updatefeb2008.zip etc to my new update updatenew.zip without distracting my users.

So what i did is, I placed a .htaccess file in my folder www.mydomain.com/updates/
and wrote this code to it

RewriteEngine On
RewriteRule .*\.(rar¦zip¦exe)$ /updatenew.zip [L]

But this code is giving me a 404 error and i am not able to download any file from that folder.
Please help me.....:(

g1smd

3:44 pm on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The /updatenew.zip part, with the / at the beginning, is telling it to get that file from the root of your site.

The file isn't there, so it will say "file not found".

And again, the .* part says "match every filename in this folder" and rewrite it.

It will even match updatenew.zip and rewrite it again and again into an infinite loop.

You need the "left" to match only the exact pattern of stuff that needs to be corrected (at present it matches "all").

You need the right to contain the correct path and filename for the new file. At present it is saying to look in a different place to where the file is actually located (The "/" says get it from the root, not from the /updates/ folder).

ripejuice

6:23 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



Thank you very very very much................it worked.
The whole problem was caused due to misunderstanding / in the htaccess file.Made a new htaccess file
RewriteEngine On
RewriteRule .*\.(rar¦zip¦exe)$ /update/updatenew.zip [L]

Made a new folder "update" and put my file there.Now its working....

Thank you so much g1smd.

g1smd

6:39 pm on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But now your rule doesn't work for old ZIP files?

I'm confused.

The

.*
(which means "everything" - every filename, anywhere on the site) is also far too broad a test here.

.

This should work. It uses a redirect, not a rewrite. The files must all be for 2008 year:

RewriteRule ^update[b][a-z]{3}[/b]2008\.zip$ http://www.example.com/updates/updatenew.zip [NC,R=301,L]

It relies on the old filenames always using three letters for the month.

.

If there are other years involved, then it can be easily modified like this:

RewriteRule ^update[a-z]{3}200[b][5678][/b]\.zip$ http://www.example.com/updates/updatenew.zip [NC,R=301,L]

Now it works for 2005, 2006, 2007, and 2008 as well. It still needs three letters for the month.

.

Did you really need to make it redirect for other filetypes too?

RewriteRule ^update[a-z]{3}200[5678]\.[b](zip¦rar¦mp3)[/b]$ http://www.example.com/updates/updatenew.zip [NC,R=301,L]

It still relies on the old filenames always using three letters for the month. It will redirect for RAR and MP3 too.

.

I intensly dislike a filename like "updatenew". What are you going to call the next "new" file, the next time you have an even newer update?

To make life for your users easier in the future, why not change to a file naming system that has Year then Month, and uses four digits for the year and two digits for the month?

That will make the filenames much more clear, like "update-2009-09.zip", and they will also sort into the right order on the users hard drive.

Why not start doing that from the "2009-01" update onwards?

ripejuice

8:50 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



But the code works fine..Since I lost all my previous archives,I'm planning to pack all the updates to one file so let it be "update-2008-10.zip" and give it to my users.
So i think i can't use that "updates" folder for storing the new files since it can catch an infinite loop.

g1smd

8:57 pm on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It will not cause an infinite loop if the "pattern" that you test for does not match the file that you redirect or rewrite to.

If you use (.*) then that matches everything. Everything.

If you look at my examples above, then you will see that they exact match only a few specific URL patterns.