Wednesday, 25 May 2016

mod rewrite - public directory for .htaccess




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



/blog/show go to



/index.php/blog/show




RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]


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]

Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...