php Apache 重写 Laravel /public

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15498513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:21:17  来源:igfitidea点击:

Apache rewrite for Laravel /public

phpapachemod-rewritelaravel

提问by PiTheNumber

I am sorry about this, but my htdocs root is wrong and I can't change that. So I have to make it work in the /publicfolder.

我对此感到抱歉,但我的 htdocs 根目录错误,我无法更改。所以我必须让它在/public文件夹中工作。

I use the normal Laravel .htaccess file with the following rewrite:

我使用普通的 Laravel .htaccess 文件进行以下重写:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]

If I open http://kemtime2_neu.pr.domain.de/publicI get redirected to http://kemtime2_neu.pr.domain.de/public/http://kemtime2_neu.pr.domain.de/public/login

如果我打开,http://kemtime2_neu.pr.domain.de/public我会被重定向到http://kemtime2_neu.pr.domain.de/public/http://kemtime2_neu.pr.domain.de/public/login

How can I fix this?

我怎样才能解决这个问题?

I would love to make it work from http://kemtime2_neu.pr.domain.de/but getting it to work with http://kemtime2_neu.pr.domain.de/public/would be fine.

我很想让它工作,http://kemtime2_neu.pr.domain.de/但让它工作http://kemtime2_neu.pr.domain.de/public/就好了。

回答by PiTheNumber

I solved it partly. If I have a .htaccess in the root instead of /publicwith

我部分解决了它。如果我在根目录中有一个 .htaccess 而不是/publicwith

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php/ [L]

I can open http://kemtime2_neu.pr.domain.de/loginbut the images and css is still wrong. I need to check first if the files exist in /public. I think this is a new question.

我可以打开,http://kemtime2_neu.pr.domain.de/login但图像和 css 仍然是错误的。我需要先检查文件是否存在于/public. 我认为这是一个新问题

回答by Christian

I use the 3 solution of this post and works fine:

我使用了这篇文章的 3 解决方案并且工作正常:

http://web.archive.org/web/20130320184846/http://forums.laravel.io/viewtopic.php?id=1258

http://web.archive.org/web/20130320184846/http://forums.laravel.io/viewtopic.php?id=1258

Solution 1 - Alternate installation path with symlink.

解决方案 1 - 带有符号链接的备用安装路径。

This is the preferred solution and in general an all-around good idea. It's possible to install your application to a folder unrelated to public_html/ and then symlink the public folder to the public_html/ path.

这是首选的解决方案,通常是一个全面的好主意。可以将您的应用程序安装到与 public_html/ 无关的文件夹,然后将公共文件夹符号链接到 public_html/ 路径。

For example:

例如:

Install your application to /home/applications/mysite.com

将您的应用程序安装到 /home/applications/mysite.com

Imagine that your DocumentRoot points to /var/www/vhosts/mysite.com/httpdocs

想象一下你的 DocumentRoot 指向 /var/www/vhosts/mysite.com/httpdocs

Remove the httpdocs folder from the mysite.com vhosts folder then connect the two with a symlink: ln -s /home/applications/mysite.com/public /var/www/vhosts/mysite.com/httpdocs

从 mysite.com vhosts 文件夹中删除 httpdocs 文件夹,然后使用符号链接将两者连接起来: ln -s /home/applications/mysite.com/public /var/www/vhosts/mysite.com/httpdocs

Solution 2 - .htaccess with mod_rewrite

解决方案 2 - .htaccess 与 mod_rewrite

This solution enables you to drop Laravel into your public folder then use a .htaccess file to redirect requests to the public folder. This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.

此解决方案使您可以将 Laravel 放入公用文件夹,然后使用 .htaccess 文件将请求重定向到公用文件夹。此解决方案将您的应用程序和核心系统代码放入一个可公开访问的文件夹中。我们不鼓励您使用任何 PHP 框架执行此操作。

Step 1. Place Laravel in your document root folder.

步骤 1. 将 Laravel 放在您的文档根文件夹中。

Step 2. Place the following .htaccess file in your document root folder.

步骤 2. 将以下 .htaccess 文件放在您的文档根文件夹中。

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/ [L]
</IfModule>

Step 3. Make sure that you manually set your 'url' configuration in application/config/application.php otherwise Laravel will generate incorrect URLs. Make sure that each of your environments have the correct application.url configuration. For more information on environment-specific configurations see: http://laravel.com/docs/install#environments

第 3 步。确保您在 application/config/application.php 中手动设置了“url”配置,否则 Laravel 将生成不正确的 URL。确保您的每个环境都具有正确的 application.url 配置。有关特定于环境的配置的更多信息,请参阅:http: //laravel.com/docs/install#environments

Solution 3 - merge the public folder into the installation root

解决方案 3 - 将公共文件夹合并到安装根目录中

This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.

此解决方案将您的应用程序和核心系统代码放入一个可公开访问的文件夹中。我们不鼓励您使用任何 PHP 框架执行此操作。

Copy the contents of the public/ folder into your Laravel installation folder then change this line in your index.php file from:

将 public/ 文件夹的内容复制到 Laravel 安装文件夹中,然后将 index.php 文件中的这一行更改为:

require '../paths.php';

to

require 'paths.php';

Keep in mind that any bundles, libraries, or other types of third-party code may not be designed to be publicly accessible.

请记住,任何捆绑包、库或其他类型的第三方代码可能都不是可公开访问的。

Note: It's also important to note that your bundles/ and public/bundles/ directories will now conflict. When using this approach you may want to not use artisan's bundle:publish task without knowing exactly what your bundles want to publish.

注意:同样重要的是要注意您的 bundles/ 和 public/bundles/ 目录现在将发生冲突。使用这种方法时,您可能不想使用 artisan 的 bundle:publish 任务,而无需确切知道您的 bundle 想要发布什么。