Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

.htaccess - Install Laravel 5 app in subdirectory with htaccess

The Setup

I am trying to install a laravel 5 app in this directory on my server:

public_html/myapp/

And I want it to be accessed at this URL:

http://www.example.com/myapp/

I have created this .htaccess rule and placed it in the myapp folder in order to redirect requests :

RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]

Problem:

In my browser when I try to access the app I get redirected URLs like:

  • http://www.example.com/myapp/public/index.php
  • http://www.example.com/myapp/public/index.php/dashboard

instead of nice pretty URLs like:

  • http://www.example.com/myapp/
  • http://www.example.com/myapp/dashboard

What do I need to change in my .htaccess file to get proper URLs working?


Update: The laravel app can be moved elsewhere if needed, as long as the desired URL structure above (with the app accessible at example.com/myapp/) can be achieved.

Update: When I try to access example.com/myapp/dashboard I receive the following error message in the browser: NotFoundHttpException in compiled.php line 7793: with a stack trace that starts with:

in compiled.php line 7793
at RouteCollection->match(object(Request)) in compiled.php line 7066
at Router->findRoute(object(Request)) in compiled.php line 7038
at Router->dispatchToRoute(object(Request)) in compiled.php line 7030
at Router->dispatch(object(Request)) in compiled.php line 1989
at Kernel->IlluminateFoundationHttp{closure}(object(Request))

Update: The following .htaccess file is located in the public folder:

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

    RewriteEngine On

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

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

</IfModule>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...