簡體   English   中英

如何使.htaccess將所有請求從URL重定向到get.php?u =但不包含現有文件

[英]How to make a .htaccess to redirect all request from the url to get.php?u= but exclusing exsisting files

我是.htaccess新手,但我遇到無法解決的問題。

我有我的網站URL(對於此示例https://example.com ),我希望每次訪問https://example.com/example都重定向到get.php?u=example但如果是真實文件,例如: https://example.com/index.html : https://example.com/index.html

我嘗試過使用重定向和異常,但它們都沒有起作用。

1)在get.php的頂部添加html文件名數組,進行in_array檢查,如果為true則讀取並回顯html文件內容

$files = ['index.html', 'example.html'];

if(in_array($_GET['u'], $files))
 {
  echo file_get_contents($_GET['u']);
  exit;
 }

或2)使用通用標識符為文件添加前綴,並在.htaccess中添加條件以不重定向匹配的前綴URL

您可以在.htaccess文件頂部使用mod_rewrite。 例如:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /get.php?u=$1 [R,L]

這會以/example形式將URL臨時(302)重定向到/get.php?u=example ,除非/example映射到文件系統上的物理文件。 (目錄仍將被重定向。)

但是,如果您想改寫 URL(即,不要在瀏覽器的地址欄中更改URL),則將RewriteRule指令更改為:

RewriteRule (.*) get.php?u=$1 [L]

即。 刪除Rredirect )標志和斜杠前綴(不必要)。

暫無
暫無

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

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