Forum Moderators: phranque
I've got a list of about 500,000 files. They are named as:
uk-az-aaa.php
fr-az-abh.php
gr-az-agj.php
I need to dump them into a single sub-directory .com/subdir/uk-az-aaa.php and access them though the URL .com/uk-az-aaa.php and .com/fr-az-abh.php etc.
Since I do not want to clutter the root with 500,000 files, how do I go about creating a htaccess rule that would internally rewrite these to the sub-directory?
Thanks
I'd suggest putting the files into subdirectories by country-code, at least.
This rewrite is fairly trivial. Please review the documents and tutorials cited in our forum charter, and see the threads in our forum library. Also try searching this site for "rewrite subfolder rewriterule" (also try "subdirectory" instead of "subfolder") and that should turn up a few hundred previous threads. Post your code back here with specific questions if you have any trouble during testing.
Thanks,
Jim
Yes, the server is a bit slow. I'm working on a solution to solve that.
I think I've figured out the rewrite rule; where country is the prefix for the .php's and country is the directory. I've dumped this code in the .htaccess at the root. Seems to work.
RewriteRule ^country-*([^/\.]+).php/?$ http://www.example.com/country/$0 [NC]
Thanks!
[edited by: jdMorgan at 4:36 am (utc) on April 4, 2008]
[edit reason] example.com [/edit]
There's really no need to do a redirect here, a single internal rewrite rule might be neater, and will certainly be faster:
RewriteRule ^([a-z]{2})-([^/.]+)\.php/?$ /$1/$2.php [L]
Your server will be happiest if you keep 1000 files or fewer in any one directory. There are many techniques to split large numbers of files into many directories, the simplest being to use the letters in the "filename" as directory levels. For example, abc1234.php would be stored as /a/b/c/abc1234.php, while xyz0987.php would be stored as /x/y/z/xyz.php.
In your case, you could use a similar technique if the number of files per directory is still large, even after splitting by country-code:
RewriteRule ^([a-z]{2})-([a-z])([a-z])-([^/.]+)\.php/?$ /$1/$2/$3/$4.php [L]
URL-path -------> Filepath
uk-az-aaa.php --> /uk/a/z/aaa.php
fr-az-abh.php --> /fr/a/z/abh.php
gr-az-agj.php --> /gr/a/z/agj.php
Of course, you could also "keep" all or part of the original filename intact *and* sort the files into subdirectories, as implied by the description above. It all depends on what makes sense for your files, and how you code the rule. What I mean is that you could just as well do this:
URL-path -------> Filepath
uk-az-aaa.php --> /uk/a/z/uk-az-aaa.php
fr-az-abh.php --> /fr/a/z/fr-az-abh.php
gr-az-agj.php --> /gr/a/z/gr-az-agj.php
Jim