.htaccess redirect from site root to public folder, hiding "public" in URL?
Place this code in /My-Project/.htaccess
:
RewriteEngine OnRewriteBase /My-Project/RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]RewriteRule ^ %1 [L,NE,R=302]RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Add the following to the first line of your .htaccess
DirectoryIndex public/index.php public/index.html
I made some changes to @anubhava's response to working on localhost and with friendly URLs.
Directory structure:
- / (localhost root folder)
- myapp/
- public/
- index.php
- core/
- some core files ...
- myapp/
myapp/.htaccess (myapp root folder)
RewriteEngine OnRewriteBase /myappRewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]RewriteRule ^ %1 [L,NE,R=302]RewriteRule ^(.*)$ public/index.php?$1 [L,QSA]
myapp/public/.htaccess
RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?$1 [L,QSA]
myapp/public/index.php
<?phpecho 'Hello<br>';echo $_SERVER['QUERY_STRING'];
Some requests:
Hello
http://localhost/myapp/post/new
Hello
post/new