How to Redirect a Subdirectory to a Public Folder in cPanel

Posted by

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

When deploying PHP or Laravel applications on shared hosting, itโ€™s common to keep your application files outside the web root and serve only the public directory. If your app is in public_html/crm and the public entry point is in public_html/crm/public, youโ€™ll want users who visit https://www.cmsgalaxy.com/crm/ to be redirected to https://www.cmsgalaxy.com/crm/public/. Hereโ€™s how to do it, with troubleshooting for common errors like directory listings, 404, and 403 errors.

Understanding the Problem

By default, when someone visits https://www.cmsgalaxy.com/crm/, Apache serves the contents of public_html/crm/. If thereโ€™s no index.php or index.html file, Apache will show a directory listing (as in your first screenshot). If directory listing is disabled, you might see a 403 Forbidden error (third screenshot). If you try to use rewrite rules that donโ€™t match or point to missing files, youโ€™ll see a 404 Not Found error (second screenshot).

The goal is to redirect all requests for /crm/ to /crm/public/.

Step 1: Prepare the Directory Structure

  • Your application should be in public_html/crm/.
  • The web-accessible files (like index.php, assets, etc.) should be in public_html/crm/public/.

Step 2: Create or Edit the .htaccess File in /crm

Open cPanelโ€™s File Manager and navigate to public_html/crm/.
If there isnโ€™t already a file named .htaccess, create one.
If it exists, edit it.

Paste the following code:

textRewriteEngine On

# Redirect only if the request is exactly /crm or /crm/
RewriteCond %{REQUEST_URI} ^/crm/?$
RewriteRule ^$ /crm/public/ [R=302,L]

This tells Apache:

  • If the request is for /crm or /crm/, redirect the browser to /crm/public/.
  • The [R=302,L] flag means this is a temporary redirect; change it to [R=301,L] for a permanent redirect after testing.

Step 3: Save and Test

  • Save the .htaccess file.
  • Visit https://www.cmsgalaxy.com/crm/ in your browser.
  • You should be redirected to https://www.cmsgalaxy.com/crm/public/.

Step 4: Troubleshooting Common Issues

If You Still See a Directory Listing

  • Make sure your .htaccess file is named correctly (with a dot at the start, no extension).
  • Check file permissions: .htaccess should be 644, and the crm folder should be 755.
  • Confirm that mod_rewrite is enabled on your server. Most cPanel hosts have this enabled by default.
  • If you have a parent .htaccess in public_html/, make sure it isnโ€™t interfering. Parent rules can override or block subdirectory rewrites76.

If You See a 403 Forbidden Error

  • This usually means permissions are incorrect or .htaccess syntax is broken.
  • Double-check that the .htaccess file is readable and the folder is accessible.
  • Try removing all other rules from .htaccess except the redirect to isolate the issue.

If You See a 404 Not Found Error

  • Ensure that the public folder exists inside crm and contains an index.php or index.html.
  • Try accessing https://www.cmsgalaxy.com/crm/public/ directly. If that works, your redirect is fine.

Step 5: Alternative PHP Redirect

If .htaccess is not working due to server restrictions, create a file named index.php in public_html/crm/ with the following content:

php<?php
header("Location: /crm/public/");
exit;

This will redirect all visitors who land on /crm/ to /crm/public/ using PHP.

Step 6: Prevent Directory Listing

You may also want to prevent directory listing for security. Add this line to your .htaccess:

textOptions -Indexes

Step 7: Final Checklist

  • .htaccess file is in public_html/crm/
  • File permissions are correct (.htaccess = 644, folders = 755)
  • The public folder exists and is accessible
  • No conflicting rules in parent .htaccess
  • mod_rewrite is enabled

Step 8: Going Further

If you want to redirect all traffic under /crm (not just /crm/), you can expand the rule, but for most Laravel or PHP apps, only the base path needs to redirect to /public/. For more advanced routing, use internal rewrites, but for simple redirection, the above is safest and most reliable4913.

Conclusion

Redirecting /crm to /crm/public on cPanel is straightforward with a simple .htaccess rule. This approach keeps your application secure and ensures users always land on the correct entry point. If you run into errors, check permissions, syntax, and server configuration. For persistent issues, contact your hosting provider to confirm that .htaccess and mod_rewrite are supported.


Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x