Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the root folder via .htaccess

I've got a shared hosting account associated with a domain name and the root folder (correct me if that's the wrong term) is set to / so that all files on the server are public / accessible through the browser.

Can I use .htaccess or something to change the root folder to something like /example.com/public/?

like image 833
Emanuil Rusev Avatar asked Apr 26 '11 14:04

Emanuil Rusev


People also ask

What can I do with an .htaccess file?

htaccess file is a powerful website file that controls high-level configuration of your website. On servers that run Apache (a web server software), the . htaccess file allows you to make changes to your website's configuration without having to edit server configuration files.

Do I need a .htaccess file?

The . htaccess is not required for having a general website. That file simply allows you to make changes in the way your website behaves for example banning people from accessing your site or redirecting an old dead link to a new page.


2 Answers

If I'm understanding correctly, the following should work

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

This will redirect all requests that do not begin with /public/ to URL that does.

Hope that helps.

like image 195
clmarquart Avatar answered Oct 02 '22 12:10

clmarquart


The DocumentRoot directive can't be set in a .htaccess file, only in the server config. As you most likely don't have the privileges to modify the server settings your only solution is to use some rewrite magic as clmarquart already mentioned.

like image 23
acme Avatar answered Oct 02 '22 12:10

acme