Pergunta

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.

Foi útil?

Solução

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]

Outras dicas

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/.htaccess (myapp root folder)

RewriteEngine On
RewriteBase /myapp

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

myapp/public/.htaccess

RewriteEngine On
RewriteBase /

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

myapp/public/index.php

<?php
echo 'Hello<br>';
echo $_SERVER['QUERY_STRING'];

Some requests:

http://localhost/myapp/

Hello


http://localhost/myapp/post/new

Hello

post/new

this works for me all the time. Create a .htaccess in your root directory outside of the public folder then add these 3 lines and boom problem solve.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

I was looking for a same solution for my "pet project" deployed on a free web hosting Infinityfree.

The directory structure (before):

htdocs/
   ├─ public/
   └─ src/

None of the answers above separately didn't help me and caused 404 or 503 http errors or just showed me folders of a project.

SandroMarques' answer encouraged me to try add two .htaccess files. After multiple attempts, I found a solution that worked for me. I used anubhava's and SandroMarques' answers.

The directory structure (after):

htdocs/
   ├─ public/
   │   └─.htaccess //second file
   │
   ├─ src/
   └─.htaccess //first file

First .htaccess file:

RewriteEngine On
RewriteBase /

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

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

Second .htaccess file:

RewriteEngine On
RewriteBase /

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

Maybe others will find this helpful.

ok so you can do this , with symfony 5, if you want to put a .htaccess to redirect the access to public/index.php file you have this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php [QSA,L]
and you can add this to redirect to https if you have a certificate for let's encrypt for example :
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top