簡體   English   中英

使用.htaccess進行重定向而不更改URL

[英]Redirect Without Changing URL using .htaccess

我想從圖片網址重定向到頁面網址。

nevil120.herobo.com/images/Chrysanthemum.jpgnevil120.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.

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