[英]How to make certain javascript files render from php using htaccess
[英]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]
即。 刪除R
( redirect
)標志和斜杠前綴(不必要)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.