我在Hostgator共享虚拟主机上设置了Laravel,我对如何在我的网站上解析链接有疑问。
我正在关注如何在Laravel中创建链接的简单教程,而不需要生成更多实际的.php文件,但是访问这些链接的方法我必须做www.samplewebsite.com/index.php/contact
如何将我的.htaccess设置为该链接变为www.samplewebsite.com/contact.php
?
- 更新 -
我的目录结构如下:
/
- 这包含我的laravel安装作为我的php
,perl5
,etc
和我的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]
我希望这会有所帮助。
答案 0 :(得分:0)
使用.htaccess
文件夹中的Laravel /public
。
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>
答案 1 :(得分:0)
您需要对您网站的所有查询转到/public/index.php文件。 index.php文件将触发Laravel启动并遍历您的路由。如果您在路线文件中定义了“/ contact”路线,那么www.samplewebsite.com/contact将路由到您所指向的路线。根据我的阅读,最佳做法是将应用程序放在根文件夹之外,然后在路由中放置别名,该路由重定向到应用程序的/public/index.php,它位于服务器上。这样,当您推送新代码时,您可以先推送代码,然后在所有内容启动并运行后更改别名。如果你需要回滚它也会使这更容易。
我不太喜欢调整Apache服务器,并且更喜欢使用类似Heroku的东西,我可以在procfile中将webroot放到/public/index.php中。如果您刚刚开始使用Laravel并且我上面所说的内容没有多大意义,我建议您使用Heroku root,它在开发规模上是免费的。
以下是使用Laravel MySql / PHP堆栈部署到Heroku的精彩教程:https://mattstauffer.co/blog/installing-a-laravel-app-on-heroku