我希望 public/ 檔案夾是根目錄,但我想忽略 url 中的所有內容,但可以訪問 css、javascript、images 檔案夾中的檔案。
例子:
http://localhost/訪問public/檔案夾
http://localhost/css/style.css或http://localhost/js/script.js或http://localhost/images/funny.gif可訪問
我打字的時候想要 http://localhost/news/1?test=helloword
news/1?test=helloword 需要被忽略,但在 PHP 中使用 parse_url() 函式時,我必須有權訪問所有變數,例如 ['path'],['query;],...
輸出:
['path'] => '/news/1'
['query'] => {
['test'] => 'helloword'
}
我正在使用 PHP 使服務器php -S localhost:8000 -t public/運行良好
但是當我進入apache2服務器時它不起作用
我在互聯網上找到了這個 .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
只是不要忽視 [path],[query],...
希望我不是白癡問這個:/
uj5u.com熱心網友回復:
你能試試這個嗎?我已經給出了一個重定向到索引頁面的示例,因此您可以對 index.php 進行一些修改。
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) /? [R=301,L]
uj5u.com熱心網友回復:
我正在使用 PHP 來制作服務器
php -S ...
PHP 內置的網路服務器不是 Apache,因此不使用.htaccess. 您需要使用 Apache ( .htaccess) 才能將請求重寫到/public子目錄。
RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC] RewriteRule ^ %1 [L,NE,R=302] RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
這些指令走在正確的軌道上,除了它們還會重寫您的 CSS、JS 和影像。所以你需要一些額外的條件來排除這 3 個子目錄。
例如:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule !^(public|css|js|images)($|/) public/index.php [L,NC]
這假設需要在public子目錄之外訪問的唯一靜態資源包含在提到的 3 個子目錄中。這避免了對檔案系統檢查的需要。
然后,您決議$_SERVER['REQUEST_URI']中/public/index.php提取所請求的URL資訊。
更新:我RewriteCond在最后一條規則之前洗掉了指令,因為可以在RewriteRule指令本身中執行檢查。
注意:重定向(第一條規則)僅用于 SEO,而不是該程序的組成部分(因為您應該鏈接到沒有 的 URL /public)。最終,一旦您確認它按預期作業,此重定向應該是 301(永久)重定向。
uj5u.com熱心網友回復:
感謝您的幫助,但我已經找到了解決方案。:)
專案檔案夾:
config/
public/
- css/
- js/
- index.php
src/
我在專案根檔案夾中使用了這個:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
在public/檔案夾中使用這個:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteRule \.(js|css|svg|gif|jpg|jpeg|png)$ - [L]
RewriteRule ^(.*)$ index.php?router=$1 [QSA,L]
并且效果很好,對于有此問題的人,這是解決方案
這是我的代碼決議網址:https : //ibb.co/r6HR1Qd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/380796.html
下一篇:.htaccess重定向太多次
