By default, Hostinger serves your website files directly from the public_html directory. However, there are several scenarios where you might want to set a subfolder as your website's "root" directory:
- Project Isolation: Managing multiple projects within a single hosting plan.
- Cleaner File Structure: Keeping your core application files separate from logs or other system files.
- Seamless Deployment: Developing a new version of your site in a folder like
/v2and launching it without moving hundreds of files.
In this guide, we will walk you through the process of using an .htaccess file to redirect your main domain to any subfolder of your choice.
1. The Core Concept: How It Works
Since the public_html folder is a system-defined directory that cannot be renamed, we use a "rewrite" strategy:
- Place your files in a subfolder (e.g.,
public_html/main_site). - Configure a rewrite rule in the primary
.htaccessfile (located inpublic_html). - The Result: When a visitor types
yourdomain.com, the server silently serves files from the subfolder without changing the URL in the browser's address bar.
2. Preparing Your Folder Structure
Before editing any files, ensure your directory is organized. Here is a typical example:
public_html/
├── .htaccess ← The "Router" (we will edit this)
├── main_site/ ← Your actual website folder
│ ├── index.php
│ ├── assets/
│ └── ...
Pro Tip: Replace
main_sitewith your preferred folder name, such asapp,web, ordist.
3. Configuring the .htaccess File
Step 1: Access the File Manager
- Log in to your Hostinger hPanel.
- Navigate to Files → File Manager.
- Enter the
public_htmldirectory.
Step 2: Edit or Create .htaccess
- If it exists: Right-click the
.htaccessfile and select Edit. - If it doesn’t exist: Click the "New File" icon and name it exactly
.htaccess.
4. Implementation: The Rewrite Rules
Option A: Standard Redirection
Use this code if you have a standard HTML or PHP site. Copy and paste this into the .htaccess file inside public_html:
RewriteEngine On
# 1. Replace example.com with your actual domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# 2. Replace 'main_site' with your subfolder name
RewriteCond %{REQUEST_URI} !^/main_site/
# 3. Handle the rewrite
RewriteRule ^(.*)$ /main_site/$1 [L]
Option B: For WordPress, Laravel, or Other Frameworks
If your website (inside the subfolder) has its own .htaccess file for routing (like WordPress Permalinks or Laravel's index.php routing), you don't need to change the subfolder's file.
The primary .htaccess in public_html acts as a gateway, while the .htaccess inside your subfolder handles the internal application logic.
5. Troubleshooting Common Issues
If you encounter a 500 Internal Server Error or a "Too Many Redirects" loop, check the following:
- Case Sensitivity: Linux servers treat
Main_Siteandmain_siteas different folders. Ensure the names match exactly. - Trailing Slashes: Ensure your
RewriteCondfor the folder name starts with/and ends with/. - Existing Rules: If you have existing code in your
.htaccess(like SSL forced redirects), place the subfolder rewrite rules at the very top of the file. - Missing Index: Ensure an
index.phporindex.htmlfile actually exists inside your subfolder.
6. Summary: Why Use This Method?
Using a subfolder redirect is a professional way to manage your hosting environment. It provides flexibility for future updates and keeps your public_html root clean, especially if you plan to host secondary tools (like a blog or a staging site) alongside your main application.