Ad

.htaccess - Not Routing To Public Folder

- 1 answer

I seem to be having a problem with my laravel installation, I am trying to make the url route to the public folder when you type domainname.com/laravel instead of showing the directory.

However when I put

<IfModule mod_rewrite.c>
   RewriteEngine On

   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

into the .htaccess within the laravel directory; it just comes up with this.

Please tell me how I can fix this.. because I really do not know.

Ad

Answer

It sounds like what you are trying to do is host Laravel in a subdirectory of your website. Most of the time, Laravel apps are hosted in the root directory of the website, i.e. yourdomain.com. However, you want your Laravel app to show up in yourdomain.com/laravel. Correct? First, a warning:

This is NOT the recommended way to host a Laravel app

However, if you really want to move things into a subdirectory, here's one way you can start.

Create subdirectory within public/ and move core Laravel files there

By default, a clean Laravel install has the following files in the public/ folder:

public/
  - .htaccess
  - favicon.ico
  - index.php
  - robots.txt
  - web.config

Create a subdirectory and move these files into it, e.g. if your subdirectory was called laravel/ then your new structure would look like:

public/
  - laravel/
    - .htaccess
    - favicon.ico
    - index.php
    - robots.txt
    - web.config

It is important that you do NOT change the contents of the .htaccess file that comes with Laravel. Otherwise your site definitely won't work.

Update index.php so it can still find the Laravel core

Inside public/laravel/index.php you'll need to update all of the lines that look like:

require __DIR__.'/../bootstrap/autoload.php';

to look like:

require __DIR__.'/../../bootstrap/autoload.php';

There should be two lines that need to be changed.

At this point, if someone were to visit yourdomain.com/laravel they would see the Laravel welcome screen. You will also be able to create routes the way you normally would within app/Http/routes.php. For example, if your routes file looked like:

Route::get('/', function () {
    return view('welcome');
});

Route::get('/hiya', function() {
    return 'hiya!';
});

Then someone who visits yourdomain.com/laravel/hiya would see the word "hiya" on the screen.

This may cause you LOTS of problems

Like has been said, Laravel is not typically meant to be run from a subdirectory, so using it in the way that I have described may cause lots of problems and unexpected behaviors.

However, you will still be able to host static files in your public/ folder and serve them the way you would any website.

Ad
source: stackoverflow.com
Ad