Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect from site root to public folder, hiding "public" in URL?

Bellow is my site directory structure:

htdocs/
    My-Project/
        public/
            index.php
            css/
            img/
            js/
        src/
            config.php
            templates/
            library/

I do not want any direct user access to any files inside the src directory. src contains my templating and backend logic scripts.

I want to set up my .htaccess so that when the user goes to my website (root folder My-Project), they are redirected to My-Project/public/index.php. I would like the URL to simply look like My-Project.com while on the index page.

Any help? I am hesitant to modify my .htaccess file as I see a lot of conflicting advice around here and on the net as to the best way to do this.

like image 288
tdc Avatar asked Sep 02 '25 05:09

tdc


1 Answers

Place this code in /My-Project/.htaccess:

RewriteEngine On
RewriteBase /My-Project/

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
like image 166
anubhava Avatar answered Sep 04 '25 18:09

anubhava