Forum Moderators: phranque

Message Too Old, No Replies

How to Access SubDirectory Files at the Root?

         

HoboTraveler

6:11 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



Hi All,

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

jdMorgan

8:40 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



500,000 files in one directory? May I ask, is your server slow? I'd expect so...

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

HoboTraveler

7:15 am on Apr 3, 2008 (gmt 0)

10+ Year Member



Hello 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]

christopherwoods

10:02 am on Apr 3, 2008 (gmt 0)

10+ Year Member



How come $0 works as a variable placeholder when I always thought the numbering started with $1? (not saying your code is wrong at all, it's quite neat and a half-solution to a problem I faced a while back which resulted in me doing the opposite of what you're doing out of frustration... I'm curious though :)

jdMorgan

3:49 am on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$0 is undefined, and just might happen to contain a value that will work. Seems it must be so in this case...

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]

This assumes that all country-codes are two letters, lowercase.

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

HoboTraveler

1:59 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



Thanks for the internal rewrite rule idea Morgan. Btw, I am curious to know what the [L] parameter does? I could not seem to find this one on the Apache docs.

TIA

coopster

8:20 pm on Apr 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Last rule. Stop the rewriting process here and don't apply any more rewrite rules.
RewriteRule [httpd.apache.org]