MrMadBacon MrMadBacon - 2 months ago 8
Apache Configuration Question

How to redirect all requests with .htaccess

So, here's what my folder structure is like:

/
/public/
/index.php
/app/
.htaccess


I want that when I type
www.example.com
the request go to the
public\index.php
file but don't display the
public
.
And if I go to
www.example.com\app
it goes to
\public\app
but only shows
www.example.com\app
.

Is that possible with .htaccess?

Answer

In your site root .htaccess you can use this:

DirectoryIndex index.php
RewriteEngine On

# add trailing slash in front of directories
RewriteCond %{DOCUMENT_ROOT}/public/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

# internally forward to /public/...
RewriteRule !^public/ public%{REQUEST_URI} [L,NC]