4

I've set up a Laravel project on a shared hosting where the document root can't be set to Laravel's "public" folder. To get around this, I use the following .htaccess file:

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

This works fine. I can access my project through "https://domain.tld" now. However, the project is also still accessible through "https://domain.tld/public/" which is what I would like to prevent. I haven't been able to find an answer to this question, so I was wondering if this is possible at all using only .htaccess.

Metoniem
  • 239
  • 2
  • 15
  • You could try to rename `public` folder to `public_html` or whatever name used in your hosting. https://stackoverflow.com/questions/30198669/how-to-change-public-folder-to-public-html-in-laravel-5 – HTMHell Aug 05 '19 at 15:51
  • You can try to redirect `https://domain.tld/public/` to `https://domain.tld`. – N'Bayramberdiyev Aug 05 '19 at 15:52
  • @HTMHell Wait a second..that could actually work. Currently everything is in public_html (which is the doc root), but maybe I could move everything up 1 directory, and do as you said? – Metoniem Aug 05 '19 at 15:52
  • @N'Bayramberdiyev Yup I've been trying that, maybe I'm doing it wrong, but it gives me an infinite loop of redirects – Metoniem Aug 05 '19 at 15:53
  • @Metoniem Yes, that would probably be a better solution. – HTMHell Aug 05 '19 at 15:54
  • I'm honestly not sure why I hadn't thought of that @HTMHell, that might require a bit of editing in other Laravel files though? I'll have to check that out – Metoniem Aug 05 '19 at 15:56
  • I suggest put your `public/` in `/mydomain.tld/public_html/` then the whole laravel app in a directory on the same level as `public_html`, but don't forget to update your `public/index.php` to point to that laravel app directory, that's the way I do it on shared hosting – xDiff Aug 05 '19 at 16:28
  • @xDiff I'll try that too! thanks – Metoniem Aug 05 '19 at 16:48
  • @Metoniem Is it must that you should use htaccess? If you want I can show you another way. ok? – Abdulla Aug 05 '19 at 17:08
  • @Saularis not a must, but preferred! feel free to answer the question with your way, I've been experimenting with methods from the comments but haven't got one to work yet – Metoniem Aug 05 '19 at 17:11
  • Ok, wait. Submitting an answer. – Abdulla Aug 05 '19 at 17:12
  • @Metoniem Check my answer, test and update me. Btw I just tested before sharing. It works. – Abdulla Aug 05 '19 at 17:26
  • Marked my own question as duplicate because the accepted answer has answered my question as well. – Metoniem Aug 05 '19 at 17:32

1 Answers1

0

Copy two file from public folder and paste them to root folder. Two Files are: index.php and .htaccess

Then open index.php file on an editor which is pasted on the root folder. Then replace two lines with some changes as shown below. See comments on code where I said Replace this by following code

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

// require __DIR__.'/../vendor/autoload.php'; Replace this by following code
require __DIR__.'/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
// $app = require_once __DIR__.'/../bootstrap/app.php'; Replace this by following code
$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

Abdulla
  • 485
  • 6
  • 16