简体   繁体   中英

Redirect Without Changing URL using .htaccess

I want to redirect to page url from image url.

ie nevil120.herobo.com/images/Chrysanthemum.jpg to nevil120.herobo.com/index.php?image=free

The redirection should happen but url should not be changed in browser. Url should remain as nevil120.herobo.com/images/Chrysanthemum.jpg and page should load.

How to achieve it through redirection rules of .htaccess?

you could do:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} \.(jpg)$ [NC]    
RewriteRule RewriteRule ^images/([^/]+)$ index.php?image=free [QSA,L]

this will redirect all images with the .jpg extension to index.php?image=free . if you want images/Chrysanthemum.jpg to redirect to index.php?image=Chrysanthemum.jpg you could do:

RewriteRule ^images/([^/]+)$ index.php?image=$1 [QSA,L]

Note : the QSA flag appends any parameters from the original url to the new one. That means that if someone goes to images/Chrysanthemum.jpg?image=dandelion , the $_GET['image'] variable will be dandelion. To still get the first image parameter (Chrysanthemum.jpg) you can use the following regex in php:

$image = preg_replace('/image=([^&]+).*/', '$1', $_SERVER['QUERY_STRING']);

or you can just remove the QSA flag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM