Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 718
Dave Avatar asked Jan 13 '09 16:01

Dave


1 Answers

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.

like image 122
Gumbo Avatar answered Oct 17 '22 07:10

Gumbo