Reputation: 5464
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.
Upvotes: 32
Views: 99457
Reputation: 32
You need just to add .htaccess file into the public folder contain this code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
note:if you put this code on root folder it will doesn't works
Upvotes: -2
Reputation: 85
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]
Upvotes: 0
Reputation: 4704
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]
Upvotes: 12
Reputation: 104
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.
Upvotes: 6
Reputation: 6534
I made some changes to @anubhava's response to working on localhost and with friendly URLs.
Directory structure:
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:
Hello
http://localhost/myapp/post/new
Hello
post/new
Upvotes: 10
Reputation: 1085
Add the following to the first line of your .htaccess
DirectoryIndex public/index.php public/index.html
Upvotes: 11
Reputation: 784988
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]
Upvotes: 74