phpregexapache.htaccessmod-rewrite

.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.


Solution

  • 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]