设置
我正在尝试在我的服务器上的此目录中安装 laravel 5 应用程序:
public_html/myapp/
我希望通过以下 URL 访问它:
http://www.example.com/myapp/
我创建了此 .htaccess 规则并将其放置在 myapp
文件夹中以便重定向请求:
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
问题:
在我的浏览器中,当我尝试访问应用程序时,我会收到如下重定向 URL:
http://www.example.com/myapp/public/index.php
http://www.example.com/myapp/public/index.php/dashboard
而不是漂亮的 URL,例如:
http://www.example.com/myapp/
http://www.example.com/myapp/dashboard
我需要在 .htaccess 文件中更改哪些内容才能使 URL 正常工作?
<小时/>更新:如果需要,Laravel 应用程序可以移动到其他地方,只要可以实现上面所需的 URL 结构(可以通过 example.com/myapp/访问该应用程序)。
更新:当我尝试访问 example.com/myapp/dashboard
时,我在浏览器中收到以下错误消息: NotFoundHttpException in Compiled.php line 7793:
堆栈跟踪以以下内容开头:
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->Illuminate\Foundation\Http\{closure}(object(Request))
更新:以下 .htaccess 文件位于公共(public)文件夹中:
<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>
最佳答案
您可以使用符号链接(symbolic link)。
- 将您的 Laravel 应用放在其他位置(public_html 文件夹之外)
在 public_html 文件夹中创建应用公共(public)文件夹的符号链接(symbolic link)
cd/path/to/your/public_html
sudo ln -s/path/to/your/laravel_app/public myapp
You can read more about symlinks here.
关于.htaccess - 使用 htaccess 在子目录中安装 Laravel 5 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497575/