简体   繁体   English

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

[英]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 nevil120.herobo.com/images/Chrysanthemum.jpgnevil120.herobo.com/index.php?image=free

The redirection should happen but url should not be changed in browser. 重定向应该发生,但不应在浏览器中更改url。 Url should remain as nevil120.herobo.com/images/Chrysanthemum.jpg and page should load. 网址应保持为nevil120.herobo.com/images/Chrysanthemum.jpg并加载页面。

How to achieve it through redirection rules of .htaccess? 如何通过.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 . 这会将所有带有.jpg扩展名的图像重定向到index.php?image=free if you want images/Chrysanthemum.jpg to redirect to index.php?image=Chrysanthemum.jpg you could do: 如果要将images/Chrysanthemum.jpg重定向到index.php?image=Chrysanthemum.jpg ,可以执行以下操作:

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

Note : the QSA flag appends any parameters from the original url to the new one. 注意 :QSA标志会将原始网址中的所有参数附加到新网址中。 That means that if someone goes to images/Chrysanthemum.jpg?image=dandelion , the $_GET['image'] variable will be dandelion. 这意味着,如果有人转到images/Chrysanthemum.jpg?image=dandelion ,则$_GET['image']变量将为蒲公英。 To still get the first image parameter (Chrysanthemum.jpg) you can use the following regex in php: 要仍然获得第一个图像参数(Chrysanthemum.jpg),可以在php中使用以下正则表达式:

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

or you can just remove the QSA flag 或者您可以删除QSA标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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