簡體   English   中英

Laravel 5 – 從 URL 中刪除公共

[英]Laravel 5 – Remove Public from URL

我知道這是一個非常受歡迎的問題,但我一直無法找到適用於 Laravel 5 的解決方案。我一直在嘗試從 Codeigniter 遷移很長時間,但這個復雜的安裝過程一直讓我望而卻步。

我不想運行虛擬機,這在項目之間切換時似乎很尷尬。

我不想將我的文檔根目錄設置為公共文件夾,這在項目之間切換時也很尷尬。

我試過 .htaccess mod_rewrite 方法

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

這只是在compiled.php 行7610 中給了我一個Laravel NotFoundHttpException。

前陣子試L4的時候,用的是把public文件夾的內容移到root的方法。 L5 的結構完全不同,遵循相同的步驟完全破壞了 Laravel(服務器只會返回一個空白頁面)。

是否有一種在開發環境中刪除“公共”的體面方法:

  1. 適用於 L5
  2. 允許我輕松地在項目之間切換(我通常一次處理 2 或 3 個)。

謝謝

** 我使用的是 MAMP 和 PHP 5.6.2

對於 Laravel 5:

  1. 將 Laravel 根文件夾中的server.php重命名為index.php
  2. .htaccess文件從/public目錄復制到 Laravel 根文件夾。

就是這樣!



使用 Docker 為 Laravel 項目提供服務時請注意:您不需要執行任何這些操作。 當您網站的根目錄(或更常見的是: public_html )是您的 Laravel 項目時才使用此選項(當您使用 Docker 時,情況並非如此)。

別!

您真的不應該將 Laravel 根文件夾中的server.php重命名為index.php並將.htaccess文件從/public目錄復制到 Laravel 根文件夾中!!!

這樣每個人都可以訪問您的一些文件(例如.env )。 自己試試吧。 你不想那樣!


相反,您應該在您的根目錄中創建一個.htaccess文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

這將默默地將所有基本 URI 重寫到/public文件夾。 甚至所有Headers ,例如HTTP Authorization Header ,以及所有可選的 URI 參數也將被靜默傳遞到/public文件夾。

就這樣

我已經使用 2 個答案解決了這個問題:

  1. 將 server.php 重命名為 index.php(無修改)
  2. 將 .htaccess 從公共文件夾復制到根文件夾(如下面所說的 rimon.ekjon)
  3. 對於靜態,將 .htaccess 稍微更改如下:

     RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] RewriteCond %{REQUEST_URI} !(\\.css|\\.js|\\.png|\\.jpg|\\.gif|robots\\.txt)$ [NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

    如果需要任何其他靜態文件,只需將擴展名添加到先前聲明的列表中

在 Laravel 5.5 中,在根目錄中創建 .htacess 文件並放置以下代碼:- 參考鏈接

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</IfModule>

從 laravel 5 url 中刪除 public 的簡單方法。 您只需要從公共目錄中剪切 index.php 和 .htaccess 並將其粘貼到根目錄中即可,將 index.php 中的兩行替換為

require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

注意:以上方法僅適用於初學者,因為他們可能在設置虛擬主機時遇到問題,最好的解決方案是在本地機器上設置虛擬主機並將其指向項目的公共目錄。

目錄中創建.htaccess文件並放置如下代碼。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

@rimon.ekjon 說:

將 Laravel 根文件夾中的 server.php 重命名為 index.php 並將 .htaccess 文件從 /public 目錄復制到 Laravel 根文件夾。 - 就是這樣 !! :)

這對我有用。 但是 /public 目錄中的所有資源文件都找不到並且請求 url 不起作用,因為我使用了 asset() 助手。

我改變了 /Illuminate/Foundation/helpers.php/asset() 函數如下:

function asset($path, $secure = null)
{
    return app('url')->asset("public/".$path, $secure);
}

現在一切正常:)

謝謝@rimon.ekjon 和你們所有人。

2020 作者更新

不推薦這個答案。 相反,建議處理.htaccess文件。

只需在根目錄下創建.htaccess文件並將這些行添加到其中

<IfModule mod_rewrite.c>
    RewriteEngine On

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

就是這樣!

上面的代碼適用於根 public_html 文件夾。 已經說過你的核心 laravel 文件應該在 public_html 文件夾中,如果你的目錄看起來像 public_html/laravelapp/public 並且如果你把上面的代碼放在 laravelapp 中,它就不會工作。 因此,您必須將所有核心文件復制到 public_html 並將 .htaccess 文件放在那里。

如果您想將代碼保存在子目錄中,那么您可以創建一個子域,那么此代碼也適用於此。

1) 我還沒有找到在L5 中移動公共目錄的工作方法。 雖然您可以修改 bootstrap index.php中的一些內容,但似乎有幾個輔助函數基於該公共目錄存在的假設。 老實說,您真的不應該移動公共目錄。

2) 如果您使用MAMP,那么您應該為每個項目創建新的虛擬主機,每個服務於該項目的公共目錄。 創建后,您可以通過定義的服務器名稱訪問每個項目,如下所示:

http://project1.dev
http://project2.dev

可以在 laravel5 中刪除公共url。 按着這些次序:

步驟 1. 從public復制所有文件並粘貼到目錄

步驟 2.打開index.php文件替換為

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

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

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

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

並刪除所有緩存和 cookie。

從 URL 中刪除 public 的 4 種最佳方法。

如果您使用任何其他技巧從 URL 中刪除公共,例如將 server.php 的名稱更改為 index.php 並更改為核心文件路徑。 很明顯,不要那樣做。 那么為什么 Laravel 沒有給出這樣的解決方案,因為這不是一個正確的方法。

1) 在 Laravel 中使用 htaccess 從 URL 中刪除 public

通過在根目錄中添加 .htaccess 文件,您可以在不公開的情況下訪問該網站

<ifmodule mod_rewrite.c>
    <ifmodule mod_negotiation.c>
        Options -MultiViews
    </ifmodule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</ifmodule>

2)通過在本地創建虛擬主機來刪除公共

我在這里為 Window 操作系統提供演示。 但我會嘗試定義一個步驟,以便任何人都可以輕松地遵循該步驟。 您還可以在 google 上針對特定操作系統進行研究。

步驟 1:轉到 C:\\Windows\\system32\\drivers\\etc\\ 以管理員模式打開“hosts”文件。

第二步:添加如下代碼。 這里我給大家演示一個projectname.local域名demo,你可以隨意指定。 只要讓它在每個地方都保持不變。

127.0.0.1  projectname.local

第 3 步:現在轉到 xampp 用戶和 wamp 用戶的C:\\xampp\\apache\\conf\\extra "C:\\wamp\\bin\\apache\\Apache2.4.4\\conf\\extra"並打開"httpd-vhosts.conf"文件。 現在將以下代碼添加到其中。

注意:根據您的項目更改文檔根目錄,同時將您定義的域名添加到“hosts”文件中。

<VirtualHost projectname.local>
      ServerAdmin projectname.local
      DocumentRoot "C:/xampp/htdocs/projectdir"
      ServerName projectname.local
      ErrorLog "logs/projectname.local.log"
      CustomLog "logs/projectname.local.log" common
 </VirtualHost>

第 4 步:最后但重要的一步是重啟你的 Xampp 或 Wamp 並訪問像http://projectname.local這樣的 URL,你的 Laravel 將在沒有公共 URL 的情況下響應。


3) 通過在 Laravel 中運行命令移除 public

如果您在本地工作,則無需執行任何操作,只需從終端或命令行工具運行以下命令即可。 之后,您可以通過命令行提供的 URL 訪問您的網站。

   > php artisan serve

如果您願意在特定 IP 上運行您的項目,那么您需要運行以下命令。 如果您在 LAN 上工作,那么如果您想允許其他人從本地訪問您的網站,那么您只需要在按照命令運行 IP 地址后,通過運行“ipconfig”來使用命令行檢查您的 IP 地址。

> php artisan serve --host=192.168.0.177

如果您願意在具有特定端口的特定 IP 上運行您的項目,那么您需要使用以下命令。

> php artisan serve --host=192.168.0.177 --port=77

4)刪除托管服務器或cpanel上的public

在此處輸入圖片說明

項目完成后,您需要將項目托管在服務器上,然后您只需要將域上的文檔根目錄設置為公用文件夾即可。 檢查下面的屏幕截圖。

根據屏幕截圖,如果您在 public_html 中沒有任何項目文件夾,那么您只需要將文檔根目錄設置為"public_html/public"

參考取自這里

這是截至2018 年 5 月適用於 Laravel 5.5的最佳和最短的解決方案

  1. 只需將您的.htaccess文件從/public目錄剪切到目錄並將其內容替換為以下代碼:

     <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] RewriteCond %{REQUEST_URI} !(\\.css|\\.js|\\.png|\\.jpg|\\.gif|robots\\.txt)$ [NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ server.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] </IfModule>
  2. 只需保存.htaccess文件即可。

  3. 將您的server.php文件重命名為index.php 這就是享受!

假設您將所有其他文件和目錄放在名為“locale”的文件夾中。

在此處輸入圖片說明

只需轉到 index.php 並找到這兩行:

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

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

並將它們更改為:

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

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

最佳方法:我不建議刪除 public,而是on local computer create a virtual host point to public directoryon remote hosting change public to public_html and point your domain to this directory 原因,您的整個 Laravel 代碼將是安全的,因為它的下一級到您的公共目錄:)

方法一:

我只是將server.php重命名為index.php就可以了

方法二:

這適用於所有 Laravel 版本......

這是我的目錄結構,

/laravel/
... app
... bootstrap
... public
... etc

按照這些簡單的步驟

  1. 將所有文件從公共目錄移動到根目錄 /laravel/
  2. 現在,不需要公共目錄,因此您可以選擇立即刪除它
  3. 現在打開 index.php 並進行以下替換

需要目錄.'/../bootstrap/autoload.php';

需要目錄.'/bootstrap/autoload.php';

$app = require_once DIR .'/../bootstrap/start.php';

$app = require_once DIR .'/bootstrap/start.php';

  1. 現在打開 bootstrap/paths.php 並更改公共目錄路徑:

'public' => DIR .'/../public',

'public' => DIR .'/..',

就是這樣,現在嘗試 http://localhost/laravel/

我想添加到@Humble Learner 並注意“修復”資產的 url 路徑的正確位置是 /Illuminate/Routing/UrlGenerator.php/asset()。

更新方法以匹配:

public function asset($path, $secure = null)
{
    if ($this->isValidUrl($path)) return $path;
    $root = $this->getRootUrl($this->getScheme($secure));
    return $this->removeIndex($root).'/public/'.trim($path, '/');
}

這將修復腳本、樣式和圖像路徑。 任何資產路徑。

對於 XAMPP 用戶在不觸及 Laravel 默認文件系統的情況下從 url 中刪除 public 是為您的應用程序設置一個虛擬主機來執行此操作,只需按照以下步驟操作

  1. 打開 XAMPP 控制面板應用程序並停止 Apache。 請注意,較晚的 Windows 計算機可能會將其作為服務運行,因此請選中 Apache 模塊左側的框。

  2. 導航到C:/xampp/apache/conf/extra或 XAMPP 文件所在的任何位置。

  3. 使用文本編輯器打開名為 httpd-vhosts.conf 的文件。

  4. 在第 19 行找到 # NameVirtualHost *:80並取消注釋或刪除哈希。

  5. 在文件的最底部粘貼以下代碼:

<VirtualHost *>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
  1. 現在您可以復制並粘貼上面的代碼以添加您的虛擬主機目錄。 例如,我正在一個名為 Eatery Engine 的網站上工作,因此以下代碼段將允許我在本地安裝中使用子域:
<VirtualHost eateryengine.dev>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder
    ServerName eateryengine.dev
    ServerAlias eateryengine.dev
    <Directory "C:/xampp/htdocs/eateryengine">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
  1. 接下來轉到您的 Windows 主機文件以編輯您的主機。 該文件將位於C:/Windows/System32/drivers/etc/hosts ,其中 hosts 是文件。 用記事本打開。
  2. 查找 #localhost 名稱解析是在 DNS 本身內處理的。

127.0.0.1 localhost

本地主機名稱解析在 DNS 本身內處理。

127.0.0.1       localhost
127.0.0.1       eateryengine.dev #change to match your Virtual Host.
127.0.0.1       demo.eateryengine.dev #manually add new sub-domains.
  1. 重新啟動 Apache 並測試一切。

原始文章可以在這里找到

在 Laravel 設置中總是有一個公共文件夾的原因,所有與公共相關的東西都應該存在於公共文件夾中,

不要將您的 IP 地址/域指向 Laravel 的根文件夾,而是將其指向公共文件夾。 將服務器 Ip 指向根文件夾是不安全的,因為除非您在.htaccess寫入限制,否則可以輕松訪問其他文件。,

只需在.htaccess文件中寫入rewrite條件並安裝rewrite模塊並啟用rewrite模塊,路由中添加public的問題就解決了。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteRule ^(.*)$ public/$1 [L]
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

激活 mod_rewrite 模塊

sudo a2enmod rewrite

並重新啟動apache

sudo service apache2 restart

要在 .htaccess 文件中使用 mod_rewrite(這是一個非常常見的用例),請使用以下命令編輯默認的 VirtualHost

sudo nano /etc/apache2/sites-available/000-default.conf

在“DocumentRoot /var/www/html”下面添加以下幾行:

<Directory "/var/www/html">
AllowOverride All
</Directory>

再次重啟服務器:

sudo service apache2 restart

我知道對於這個問題有很多解決方案。 最好和最簡單的解決方案是在目錄中添加.htaccess文件。

我嘗試了我們為此問題提供的答案,但是我在 Laravel 中遇到了 auth:api 防護的一些問題。

這是更新的解決方案:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

在根目錄中創建 .htaccess 文件並添加此代碼。 一切順利

在根目錄中創建 .htaccess 文件並放置如下代碼。

<IfModule mod_rewrite.c>
 #Session timeout

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

</IfModule>

在 /public 目錄中創建 .htaccess 文件並放置如下代碼。

<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
  </IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

在根項目文件夾中創建名為.htaccess新文件,並將以下代碼放入其中:

<IfModule mod_rewrite.c>
 #Session timeout

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

</IfModule>

注意:如果您將server.php更改為index.php您還應該在上面的代碼中將其重命名

即使我不建議將 Laravel 放在根文件夾中,也有一些無法避免的情況; 對於這些情況,上述方法不適用於資產,因此我快速修復了更改 htaccess:將 server.php 復制到 index.php 后編輯 .htaccess 文件,如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    ### fix file rewrites on root path ###
    #select file url
    RewriteCond %{REQUEST_URI} ^(.*)$
    #if file exists in /public/<filename>
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
    #redirect to /public/<filename>
    RewriteRule ^(.*)$ public/$1 [L]
    ###############

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...

    #RewriteCond %{REQUEST_FILENAME} -d # comment this rules or the user will read non-public file and folders!
    #RewriteCond %{REQUEST_FILENAME} -f #
    RewriteRule ^ index.php [L]
</IfModule>

這是我必須進行的快速修復,因此歡迎任何人升級它。

Laravel 5.5

第一次安裝 Laravel 后,我遇到了著名的“公共文件夾問題”,我想出了這個解決方案,在我個人看來,它比我在網上找到的其他解決方案“更干凈”。


成就

  • URI 中沒有public
  • 保護.env文件免受好奇的人的侵害

一切都可以通過使用mod_rewrite和四個簡單規則編輯.htaccess來完成。


腳步

  1. .htaccess文件移動到主根目錄的public/.htaccess
  2. 編輯如下

我評論了所有內容,因此(我希望)那些從未使用過mod_rewrite人也應該清楚(我不是專家,恰恰相反)。 另外,要理解規則,必須清楚的是,在 Laravel 中,如果 Bill 連接到https://example.com ,則加載https://example.com/index.php 這個文件只包含命令header("refresh: 5; https://example.com/public/") ,它將請求發送到https://example.com/public/index.php 第二個index.php負責加載控制器和其他東西。

# IfModule prevents the server error if the app is moved in an environment which doesn’t support mod_rewrite
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # RULES ORIGINALLY IN public/.htaccess ---
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    # --- END

    # PERSONAL RULES ---
    # All the requests on port 80 are redirected on HTTPS
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

    # When .env file is requested, server redirects to 404
    RewriteRule ^\.env$ - [R=404,L,NC]

    # If the REQUEST_URI is empty (means: http://example.com), it loads /public/index.php
    # N.B.: REQUEST_URI is *never* actually empty, it contains a slash that must be set as match as below
    # .* means: anything can go here at least 0 times (= accepts any sequence of characters, including an empty string)
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*) /public/index.php [L]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI !== css||js||images/whatever => server loads /public/index.php, which is responsible to load the app and the related controller
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^(css|js|images|media)/(.*)$ /public/index.php [L,NC]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI == css||js||images[=$1]/whatever[=$2] => server loads the resource at public/$1/$2
    # If R flag is added, the server not only loads the resource at public/$1/$2 but redirects to it
    # e.g.: bamboo.jpg resides in example.com/public/media/bamboo.jpg
    #       Client asks for example.com/media/bamboo.jpg
    #       Without R flag: the URI remains example.com/media/bamboo.jpg and loads the image
    #       With R flag: the server redirects the client to example.com/public/media/bamboo.jpg and loads the image
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
    # --- END

</IfModule>

可以刪除以下規則(最初在public/.htaccess )。 事實上,相同的規則在最后兩條規則中以更詳細的方式進行了明確。

# Handle Front Controller...
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^ index.php [L]

編輯:我錯過了Abhinav Saraswat的解決方案,他的答案應該是公認的。 只是一個簡單明了的規則,將所有流量重定向到公用文件夾,而無需修改任何文件。

首先你可以使用這個步驟

對於 Laravel 5:

1.將 Laravel 根文件夾中的 server.php 重命名為 index.php

2.將 .htaccess 文件從 /public 目錄復制到 Laravel 根文件夾。

來源: https : //stackoverflow.com/a/28735930

按照這些步驟操作后,您需要更改所有 css 和腳本路徑,但這會很累。

解決方案建議:只需對helpers::asset函數進行細微更改即可。

為此

  1. 打開vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php

  2. 轉到第 130 行

  3. "public/".$path而不是$path

     function asset($path, $secure = null){ return app('url')->asset("public/".$path, $secure); }

問題是如果您輸入 /public 並且它仍然可以在 url 中使用,因此我創建了一個修復程序,它應該放在 public/index.php 中

  $uri = urldecode(
     parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  );

  if(stristr($uri, '/public/') == TRUE) {

    if(file_exists(__DIR__.'/public'.$uri)){

    }else{

      $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
      $actual_link = str_replace('public/', '',$actual_link);
      header("HTTP/1.0 404 Not Found");
      header("Location: ".$actual_link."");
      exit();
      return false;
   }}

這個代碼的和平將從url中刪除public並給出404然后重定向到沒有public的url

在您的根目錄中創建一個 .htaccess 文件並粘貼以下代碼。 就是這樣:P

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ ^$1 [N] RewriteCond %{REQUEST_URI} (\\.\\w+$) [NC] RewriteRule ^(.*)$ public/$1 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ server.php

我之前讀過一些文章,它工作正常,但真的不知道是否安全

    a. Create new folder local.
    b. Move all project into the local folder expect public folder.
    c. Move all the content of public folder to project root.
    d. Delete the blank public folder
    f. Edit the index file. 

編輯 index.php

require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/app.php';

我使用的另一種方法是使用 htdocs 或 www 中的 ln 命令創建符號鏈接(在 Linux 中,不知道 Win),即ln projectname/public project該站點因此可以通過 localhost/project 訪問

您可以使用各種方法從 url 中刪除 public 關鍵字。

1) 如果您使用的是專用主機並且您擁有 root 訪問權限,那么您可以使用虛擬主機從 url 中刪除 public 關鍵字。 你應該用 public 給 DocumentRoot 路徑。 因此,這將從公共目錄開始索引並將其從 url 中刪除。

<VirtualHost *:80>
    ServerAdmin info@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/{yoursourcedirectory}/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2)如果您沒有主機的root訪問權限,那么您應該在根目錄中生成一個新的.htaccess文件並將代碼如下

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>

您可以在此處獲得更多參考

將.htaccess文件添加到您的根文件夾,然后粘貼以下代碼,並將yourdomainname替換為您自己的域名

RewriteCond%{HTTP_HOST} ^(www。)上的RewriteEngine?您的域名$ RewriteCond%{REQUEST_URI}!^ / public /

RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_FILENAME}!-d

RewriteRule ^(。*)$ / public / $ 1

RewriteCond%{HTTP_HOST} ^(www。)?您的域名$ RewriteRule ^(/)?$ /public/index.php [L]

您可以先創建虛擬主機,然后在DocumentRoot中指定路徑。

示例: /var/www/html/YOUR_LARAVEL_DIRECTORY_PATH/public

您可以通過 2 個簡單的步驟輕松完成

  1. 重命名 Laravel 項目根目錄中的server.php文件。

  2. 在根目錄上創建一個.htaccess文件並將以下代碼粘貼到其中

Options -MultiViews -Indexes

RewriteEngine On
 

**#Handle Authorization Header**

RewriteCond %{HTTP:Authorization} .

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

 

**# Redirect Trailing Slashes If Not A Folder...**

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (.+)/$

RewriteRule ^ %1 [L,R=301]

#Handle Front Controller...

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]


RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^/public/

RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

我找到了這個問題的最有效的解決方案。

只需在文件夾中編輯.htaccess並編寫以下代碼。 不需要其他

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit -1
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>

請將這些行添加到您的根htaccess文件夾中,即可完成此工作

RewriteEngine on
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

我在這里嘗試了一些解決方案,並發現適用於laravel 5.2的htaccess,如下所示:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]



RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
RewriteRule ^.env - [F,L,NC]
RewriteRule ^app - [F,L,NC]
RewriteRule ^bootstrap - [F,L,NC]
RewriteRule ^config - [F,L,NC]
RewriteRule ^database - [F,L,NC]
RewriteRule ^resources - [F,L,NC]
RewriteRule ^vendor - [F,L,NC]

當您有另一個要禁止的文件或文件夾時,只需添加

RewriteRule ^your_file - [F,L,NC]

您不需要做很多事情,只需在根目錄上創建一個.htaccess文件,然后粘貼/保存以下內容即可;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM