Changing the root folder via .htaccess
You are likely referring to your Document Root when you mention the "root folder." In Apache terminology, this is the directory defined by the DocumentRoot directive in your configuration. When a request hits example.com, Apache serves files from that specific directory. If your current setup points to /, everything inside that server's public tree is accessible, which often leads to unintended exposure of sensitive files like .git directories or backup archives.
You absolutely can change where your site lives without moving every single file on the filesystem, provided you are on shared hosting that allows .htaccess overrides. The mechanism relies entirely on Apache's Alias directive within a .htaccess file to map the URL path to a different physical directory.
However, simply using an Alias directive inside a standard .htaccess file often fails on shared hosting environments for one specific reason: permissions and directory traversal rules. If you attempt to alias /example.com/public/ from the web root (/) when the actual files reside in /var/www/example.com/public, Apache might reject the request because it cannot serve files outside the DocumentRoot unless specific flags are set, or your hosting provider has disabled Alias directives in user .htaccess files for security.
The Alias Directive Strategy
The most common approach to achieve this is placing an .htaccess file inside your new target directory (e.g., /example.com/public/) and directing traffic there. However, the syntax depends heavily on how you want the URL to behave relative to the alias.
If you want example.com/file.txt to serve a file located at /public/file.txt, you typically use an Alias directive in your main server configuration (often found in httpd.conf or a virtual host file), not necessarily inside the .htaccess of the target folder. Inside the target folder's .htaccess, you would usually see:
<IfModule mod_rewrite.c>
RewriteEngine On
# This forces all requests to go to /public/... if the file doesn't exist in the alias path
# But this is a specific pattern, not the primary solution for changing roots.
</IfModule>
Wait, that approach changes how URLs are rewritten, which isn't what you asked for. You asked to change the root itself. On shared hosting, if your provider allows Alias in .htaccess, the syntax looks like this:
# Map the URL path /example.com/public/ to a physical directory
Alias /public/ "/var/www/html/example.com/public/"
<Directory "/var/www/html/example.com/public/">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
Crucial limitation: Most shared hosting providers (like cPanel/Plesk users) do not allow Alias directives in user-level .htaccess files. They only work in the global configuration. If you cannot edit the main Apache config, your options are more restricted.
Using mod_rewrite for a "Hidden" Root
If you cannot modify the global config and Alias is blocked, you might try to simulate a root change using mod_rewrite. This doesn't actually move the DocumentRoot but makes your site appear to live in a subdirectory while serving files from the root.
Place an .htaccess file in your current public root (e.g., /var/www/html/example.com/) with the following content:
RewriteEngine On
# Check if the requested file exists in the current directory
# If not, redirect to /public/ subdirectory version
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Force requests to go into the /public/ folder
# This effectively makes /example.com/file.html serve /example.com/public/file.html
RewriteRule ^(.*)$ /public/$1 [L,QSA]
The Trade-off: This method requires you to place all your actual application files inside a /public subdirectory within the original root. You are not changing the DocumentRoot; you are creating a URL rewrite rule that hides the fact that your site is in a subfolder.
The Shared Hosting Reality
If you are on a strict shared hosting plan, simply adding Alias /example.com/public/ ... to an .htaccess file located at /example.com/ will likely result in a "500 Internal Server Error" or the directive being ignored. This happens because the server strips out certain directives from user .htaccess files to prevent directory traversal vulnerabilities.
To successfully change your root folder on shared hosting without access to the main config:
- Move all your application code into a subdirectory named
public. - Ensure your web server's DocumentRoot remains the original public folder.
- Use the
mod_rewriteexample above to force all traffic into that subdirectory.
Why This Matters for Security
Changing your root or nesting your app in a /public folder is often a security best practice. It prevents attackers from accidentally accessing files they shouldn't, such as .git/config, environment variables in .env, or vendor libraries located outside the application's public face. By forcing traffic through a specific subdirectory, you create a layer of isolation between your web server's default directory and your actual application codebase.
If you are managing a Laravel project specifically, this approach is native to the framework's structure. The public folder in Laravel is designed exactly for this scenario: it contains the only files that should be publicly accessible. If you are deploying Laravel on shared hosting where you cannot change the global DocumentRoot, creating an alias or rewrite rule to point traffic directly into the public directory is the standard procedure recommended by LaravelCompany.com for deployment guides involving restricted environments.
In summary, you cannot always "change" the root folder via a simple .htaccess line on shared hosting due to security restrictions. Instead, you must restructure your file layout so that your application lives in a subdirectory and use rewrite rules or server configuration (if available) to map the URL to that location.