【问题标题】:Apache Laravel Installation - LinksApache Laravel 安装 - 链接
【发布时间】:2018-03-01 13:39:09
【问题描述】:

我正在我的 Hostgator 共享虚拟主机上设置 Laravel,但我对如何在我的网站上解析链接有疑问。

我正在学习如何在 Laravel 中创建链接而不需要生成更多实际的 .php 文件的简单教程,但访问这些链接的方法我必须做www.samplewebsite.com/index.php/contact

如何将我的 .htaccess 设置为该链接变为 www.samplewebsite.com/contact.php

-- 更新--

我的目录结构如下:

/ - 这包含我的 laravel 安装作为我的 phpperl5etc 和我的 public_html 文件夹。

我的 Laravel 安装结构如下:

/app_base/,公用文件夹为/app_base/public/

我在我的 public_html (www) 文件夹中创建了一个 Demo 文件夹,作为我的测试环境,因此我可以在开始使用正确的网站之前掌握开发。

/public_html/DEMO

-index.php-

    <?php
    /**
     * Laravel - A

 PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| 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 nice to relax.
|
*/

require __DIR__.'/../../app_base/bootstrap/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.
|
*/

require __DIR__.'/../../app_base/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');

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

$response->send();

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

我的 .htaccess 文件包含在我的 public_html 文件夹中。如下:

RewriteEngine on

<IfModule mod_rewrite.c>
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

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

RewriteCond %{HTTP_HOST} ^spotoncreative\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.spotoncreative\.net$
RewriteRule ^default\.html$ "http\:\/\/www\.spotoncreative\.net\/Splash\/" [R=301]

RewriteCond %{HTTP_HOST} ^spotoncreative\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.spotoncreative\.net$
RewriteRule ^/?$ "http\:\/\/www\.spotoncreative\.net\/Splash\/" [R=301]

我希望这会有所帮助。

【问题讨论】:

标签: php apache laravel


【解决方案1】:

使用 /public 文件夹中的 Laravel .htaccess

https://github.com/laravel/laravel/blob/master/public/.htaccess

<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>

【讨论】:

  • 我唯一拥有 .htaccess 的地方是我的 /public_html/ 文件夹。有关更多信息,请参阅更新后的问题。
【解决方案2】:

您需要将所有对您网站的查询都转到 /public/index.php 文件。 index.php 文件将触发 Laravel 启动并通过你的路由。如果您在路由文件中定义了路由“/contact”,那么 www.samplewebsite.com/contact 将路由到您指定的任何位置。根据我的阅读,最佳做法是将应用程序放在根文件夹之外,然后在重定向到 /public/index.php 的路由中放置一个别名,该路径位于您的服务器上。这样,当您推送新代码时,您可以先推送代码,然后在一切正常运行后更改别名。如果您需要回滚,它也会变得更容易。

我不太擅长调整 Apache 服务器,我更喜欢使用 Heroku 之类的东西,我可以在 Procfile 中将 webroot 放到 /public/index.php 中。如果您刚刚开始使用 Laravel,而我上面所说的没有多大意义,我建议您使用 Heroku 根,它在开发规模上是免费的。

这是一个关于使用 Laravel MySql/PHP 堆栈部署到 Heroku 的精彩教程:https://mattstauffer.co/blog/installing-a-laravel-app-on-heroku

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2022-10-04
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2017-08-10
    • 2014-11-21
    • 2012-06-27
    相关资源
    最近更新 更多