[英]Redirect Without Changing URL using .htaccess
我想從圖片網址重定向到頁面網址。
即nevil120.herobo.com/images/Chrysanthemum.jpg
到nevil120.herobo.com/index.php?image=free
重定向應該發生,但不應在瀏覽器中更改url。 網址應保持為nevil120.herobo.com/images/Chrysanthemum.jpg
並加載頁面。
如何通過.htaccess的重定向規則來實現?
你可以做:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(jpg)$ [NC]
RewriteRule RewriteRule ^images/([^/]+)$ index.php?image=free [QSA,L]
這會將所有帶有.jpg擴展名的圖像重定向到index.php?image=free
。 如果要將images/Chrysanthemum.jpg
重定向到index.php?image=Chrysanthemum.jpg
,可以執行以下操作:
RewriteRule ^images/([^/]+)$ index.php?image=$1 [QSA,L]
注意 :QSA標志會將原始網址中的所有參數附加到新網址中。 這意味着,如果有人轉到images/Chrysanthemum.jpg?image=dandelion
,則$_GET['image']
變量將為蒲公英。 要仍然獲得第一個圖像參數(Chrysanthemum.jpg),可以在php中使用以下正則表達式:
$image = preg_replace('/image=([^&]+).*/', '$1', $_SERVER['QUERY_STRING']);
或者您可以刪除QSA標志
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.