Forum Moderators: phranque
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
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.
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.?
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.
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.....:(
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).
Made a new folder "update" and put my file there.Now its working....
Thank you so much g1smd.
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?