简体   繁体   English

.htaccess-不路由到公用文件夹

[英].htaccess - not routing to public folder

I seem to be having a problem with my laravel installation, I am trying to make the url route to the public folder when you type domainname.com/laravel instead of showing the directory. 我的laravel安装似乎出现问题,当您键入domainname.com/laravel而不是显示目录时,我试图将URL路由到公用文件夹。

However when I put 但是当我把

<IfModule mod_rewrite.c>
   RewriteEngine On

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

into the .htaccess within the laravel directory; 进入laravel目录中的.htaccess; it just comes up with this. 它只是与此有关。

Please tell me how I can fix this.. because I really do not know. 请告诉我如何解决此问题。因为我真的不知道。

It sounds like what you are trying to do is host Laravel in a subdirectory of your website. 听起来您要尝试的是在网站的子目录中托管Laravel。 Most of the time, Laravel apps are hosted in the root directory of the website, ie yourdomain.com . 大多数情况下,Laravel应用程序托管在网站的根目录(即yourdomain.com However, you want your Laravel app to show up in yourdomain.com/laravel . 但是,您希望Laravel应用显示在yourdomain.com/laravel Correct? 正确? First, a warning: 首先,警告:

This is NOT the recommended way to host a Laravel app 这不是推荐的托管Laravel应用的方法

However, if you really want to move things into a subdirectory, here's one way you can start. 但是,如果您确实要将内容移动到子目录中,则可以采用以下一种方法开始。

Create subdirectory within public/ and move core Laravel files there public/创建子目录,然后在其中移动核心Laravel文件

By default, a clean Laravel install has the following files in the public/ folder: 默认情况下,全新的Laravel安装在public/文件夹中具有以下文件:

public/
  - .htaccess
  - favicon.ico
  - index.php
  - robots.txt
  - web.config

Create a subdirectory and move these files into it, eg if your subdirectory was called laravel/ then your new structure would look like: 创建一个子目录并将这些文件移动到其中,例如,如果您的子目录名为laravel/那么您的新结构将如下所示:

public/
  - laravel/
    - .htaccess
    - favicon.ico
    - index.php
    - robots.txt
    - web.config

It is important that you do NOT change the contents of the .htaccess file that comes with Laravel. 重要的是,请勿更改Laravel随附的.htaccess文件的内容。 Otherwise your site definitely won't work. 否则,您的网站肯定无法正常工作。

Update index.php so it can still find the Laravel core 更新index.php以便它仍然可以找到Laravel核心

Inside public/laravel/index.php you'll need to update all of the lines that look like: public/laravel/index.php内部,您需要更新所有看起来像这样的行:

require __DIR__.'/../bootstrap/autoload.php';

to look like: 看起来像:

require __DIR__.'/../../bootstrap/autoload.php';

There should be two lines that need to be changed. 应该有两行需要更改。

At this point, if someone were to visit yourdomain.com/laravel they would see the Laravel welcome screen. 此时,如果有人访问yourdomain.com/laravel他们将看到Laravel欢迎屏幕。 You will also be able to create routes the way you normally would within app/Http/routes.php . 您还可以像在app/Http/routes.php创建路由。 For example, if your routes file looked like: 例如,如果您的路由文件如下所示:

Route::get('/', function () {
    return view('welcome');
});

Route::get('/hiya', function() {
    return 'hiya!';
});

Then someone who visits yourdomain.com/laravel/hiya would see the word "hiya" on the screen. 这样,访问yourdomain.com/laravel/hiya就会在屏幕上看到“ hiya”一词。

This may cause you LOTS of problems 这可能会导致您遇到很多问题

Like has been said, Laravel is not typically meant to be run from a subdirectory, so using it in the way that I have described may cause lots of problems and unexpected behaviors. 就像已经说过的那样,Laravel通常不是要从子目录运行,因此以我描述的方式使用它可能会导致很多问题和意外行为。

However, you will still be able to host static files in your public/ folder and serve them the way you would any website. 但是,您仍然可以在您的public/文件夹中托管静态文件,并可以像处理任何网站一样为它们提供服务。

Its actually not a recommended way to place your laravel files when you are placing you project in a subdirectory, There is a recommended steps to correctly placing the contents of the directory . 实际上,不建议将project放置在子目录中时放置laravel文件。建议使用一些步骤来正确放置directory的内容。 First, leave the .htaccess as it was at the time of installation. 首先,将.htaccess保留为安装时的状态。 Then here are few steps you can take to make your site work. 然后,您可以采取一些steps来使您的网站正常运行。

1. You see `public` directory, copy all the contents of the it and `paste` it in your `root` directory and then remove the `public` folder from the original place.
2.  then you make a single folder and put all the contents inside newly created folder except the contents of public folder that you just pasted in your root  

a structure you will have after this in your root will be like 在此之后您将拥有的结构就像

a) contents of public folder
b) a single folder, lets say  `myproject` including all the contents except public

now, you should edit index.php like 现在,您应该像这样编辑index.php

require __DIR__.'/myproject/bootstrap/autoload.php';


$app = require_once __DIR__.'/myproject/bootstrap/app.php';

here myproject is your folder name where you put all the contents except public directory contents 这里myproject是您的文件夹名称,您在其中放置了除public目录内容以外的所有内容

Finally, clear all the cache and your program should work If you still want .htaccess to your work, i suggest you to do some search in forums 最后,清除所有cache ,您的程序应该可以运行。如果您仍然希望对工作使用.htaccess ,建议您在论坛中进行一些搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM