public directory for .htaccess

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 thought on “public directory for .htaccess

  1. user

    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.

    Reply

Leave a Reply

Your email address will not be published.