Install Laravel 5 app in subdirectory with htaccess

自古美人都是妖i 提交于 2019-11-29 09:14:09

What you can do is use a symlink.

  1. Place your laravel app somewhere else (outside the public_html folder)
  2. Create a symlink of your app's public folder inside the public_html folder

    cd /path/to/your/public_html

    sudo ln -s /path/to/your/laravel_app/public myapp

You can read more about symlinks here.

You can easily achieve this by adding additional .htaccess file in the myapp folder (this will redirect all of your requests in myapp folder to the public as it should):

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

You should also modify the .htaccess in your myapp/public directory, like this:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On    
    RewriteBase /myapp/

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
</IfModule>

The change here is that you rewrite the base path RewriteBase /myapp/

I was having the same issue, but with the RewriteBase parameter it was possible to fix it.

This is my .htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /my_app/

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

This example should work for following cases:

Follow these steps:

  1. Move everything from myapp/public folder to myapp/ folder
  2. Open myapp/index.php and update bootstrap paths to require __DIR__.'/bootstrap/
  3. Open .env and update to your base url like: APP_URL=http://example.com/myapp
  4. Update paths in all views and layout (also in the code if you have any) to use base url, like this: src="{{URL::to('/js/app.js')}}"

It's always recommended to separate public folder from the rest of your app. You can add some protection to Laravel folders by adding .htaccess files inside them that will contain "Deny from all".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!