我正在開發一個 PHP 專案。我想將除某些(/、/index.php、/video-archive.php 等)之外的所有 URL 路由到 PHP 檔案,即 /article.php
讓我更詳細地描述一下:
考慮到我在 URL 有 article.php https://example.com/article.php,當客戶端想要訪問 URLhttps://example.com/how-to-grow-beautiful-flowers.php或沒有.php擴展名時,我希望這個請求由這個https://example.com/article.phpURL 或簡單的article.php檔案處理。如果用戶想要到達https://example.com/orhttps://example.com/index.php或者https://example.com/video-archive.php他們將到達index.phpand video-archive.php。所以這條路線會有例外。
REQUEST URL PROCESSING URL
https://example.com/ https://example.com/index.php
https://example.com/index.php https://example.com/index.php
https://example.com/index https://example.com/index.php
https://example.com/video-archive https://example.com/video-archive.php
https://example.com/video-archive.php https://example.com/video-archive.php
https://example.com/how-to-grow-flowers https://example.com/article.php
https://example.com/types-of-flowers https://example.com/article.php
https://example.com/about-us https://example.com/article.php
注意:我目前正在我的計算機(本地主機)上使用 XAMPP 開發這個專案。但我計劃將來將它部署在服務器上。所以最好考慮這些資訊。
uj5u.com熱心網友回復:
您可以將所有請求重定向到/article.php期望對實際存在的檔案的請求。/index.php對和靜態檔案的示例請求/video-archive.php將加載實際檔案。試試這個
把它放在.htaccess根目錄下
RewriteEngine On
# redirect all request to article.php except existing files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ article.php [QSA,L]
然后,您可以article.php像這樣在檔案中獲取 URI
$article_url = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '.php');
// when a client hits https://example.com/how-to-grow-beautiful-flowers.php
// $article_url equals '/how-to-grow-beautiful-flowers'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425290.html
上一篇:是什么導致我的Apache2服務器中出現“403禁止錯誤”?我該如何解決?網路上的可用修復對我不起作用
下一篇:DNS_PROBE_FINISHED_NXDOMAIN–在macOS上使用Homebrew設定WordPress開發環境
