Reputation:
I have a lot of uncleverly named folders on my server. They're called things like cdn-1 cdn3 img1 and so on. I have collected all of the files in these folders and put them in one folder, called cdn. Now, I don't want users to get a 404 when they try to access a file that is at cdn-3.website.com/file/img/1.jpg. Instead, is there a way to mod_rewrite this folder so that even if a user tries to access a file at, say, cdn-7382910731293.website.com/file/1.jpeg, it'll still work? This might be far fetched, but I have seen this be done before. The only working code I've found for a solution like this is for a single file, not a folder.
EDIT: This is what I've tried so far. It just won't work. What's the problem with it?
RewriteRule ^cdn([^/]*).1$ http://cdn.website.com/$1 [L]
Upvotes: 0
Views: 102
Reputation: 158081
Maybe try make the sub-domains all point to the same place (wherever cdn.website.com points to) and then stick the following in the htaccess on that domain:
RewriteCond %{HTTP_HOST} ^(cdn-1|cdn1|img1|etc)\.website\.com
RewriteRule (.*) http://cdn.website.com/$1 [R=301,L]
That should work seamlessly, as long as the files otherwise would be in the same place. That is, only the host name is different and not also the file path after the host name. The RewriteCond is a regular expression I think, so you should be able to write it to match whatever you need.
I have used this method successfully to redirect an old domain to a new one, and to redirect example.com
to www.example.com
.
Upvotes: 0
Reputation: 4331
Simplest thing would be to use .htaccess to redirect all 404 pages to a single PHP page, which would in turn check if such a file/image exists in your new cdn folder and either server it or display a valid 404 page.
Don't forget about headers when serving images etc., too ;-)
Upvotes: 0