views:

334

answers:

1

I have a dispatcher function in index.php so URLs like:

/blog/show go to

/index.php/blog/show

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

How can I modify this so that I can dump all of my static files into a public directory but access them without public in the URL.

For example /docs/lolcats.pdf accesses

/public/docs/lolcats.pdf on the drive

I tried this

RewriteCond %{REQUEST_FILENAME} !f
RewriteRule ^(.*)$ public/$1 [QSA,L]
+1  A: 

Try this:

RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule !^public/ public%{REQUEST_URI} [L]

If you want to use it in a subdirectory below the root:

RewriteCond %{REQUEST_URI} ^/subdir(/.*)
RewriteCond %{DOCUMENT_ROOT}subdir/public%1 -f
RewriteRule !^public/ public%1 [L]

I found another solution without explicitly naming the subdirectory:

RewriteRule ^public/ - [L]
RewriteCond public/$0 -F
RewriteRule .* public/$0 [L]

The important change was using -F instead of -f as the former triggers a subrequest.

Gumbo
Thank you, this worked perfectly! I'm trying, unsuccessfully, to modify this so that it will work nested in another directory. For example, if file foo.txt is placed in DOCROOT/mysubdir/public/foo.txt, what rule lets me access it via www.example.com/mysubdir/foo.txt ?
Dave
I think this is only possible if you explicitly name the subdirectory.
Gumbo