0
votes

My ISP doesn't allow my cpanel hosted laravel site to have a custom root folder (the public folder within Laravel's structure).

This means I can't make the server see /public_html/public as the root. It has to always be /public_html

Following their guidance I have setup the following /public_html/.htaccess rule to make the Laravel public folder appear as the root folder

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

This works more or less but NOT completely. The issue is that if I request a page on my site with an ending backslash, the page url ends up as

www.domain-name.com/public/contact-us

instead of what I would expect which is

www.domain-name.com/contact-us

I assume this must be a problem with the .htaccess but I've tried various permutations and can't resolve it. I maybe wrong though, maybe it's something that ultimately needs to be fixed by a Laravel redirect after a url check.

It's driving me mad, I'd really appreciate any ideas!

1

1 Answers

1
votes

Putting the entire application inside a public_html folder is a big red flag for security purposes. You have to be very very cautions with your permissions inside this folder. Things like .env, .git, cache files, private uploads, etc are usually downloadable by default and yes hackers will be scanning for treats.

If you can upload your entire app to the root of your cpanel host and rename public to public_html (or copy the contents of public into the existing public_html if you cannot remove it)

Then you may have to modify your laravel app index.php to use this as it's public path.

In index.php under :

$app = require_once __DIR__.'/../bootstrap/app.php';

add:

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

With this the standard .htaccess should work without changes.

Hope that helps.