.htaccess image to web page redirect?

南楼画角 提交于 2019-12-04 06:24:38

问题


Is there a .htaccess script I can use to redirect the url of an image to an actual web page?

What can I do to make it so when someone accesses an image via their address bar by typing in: http://www.sitename.com/uploads/192-file-name-here.jpg or other extensions like http://www.sitename.com/uploads/235-file-name-here.png

It will instead redirect the user to the web page: http://www.sitename.com/view/192-file-name-here/

I want to still be able to place the images in image tags across the site and on external sites though.

RewriteCond %{HTTP_REFERER} !^https?:\\www\.sitename\.com
RewriteCond  %{REQUEST_URI} (.*)\.jpg$
RewriteRule images/(\d+)\.jpg  http://www.sitename.com/view/$1.html   [L,R=301]
RewriteCond %{HTTP_REFERER} ^$

Can this be modified/re-written to perform my function?


回答1:


put this in your .htaccess, it will redirect all requests that are sent directly by user.
Note: Other sites,as you mentioned can put your images in their pages.

RewriteCond %{REQUEST_URI}  ^/uploads/(\d+)-(.+)\.(png|gif|jpe?g|bmp)$  [NC]
RewriteCond %{HTTP_REFERER} ^$
RewriteRule (.*)  http://www.sitename.com/view/%1-%2/



回答2:


Yes you can always redirect images to a webpage or anything using .htaccess. But images/(\d+).*\.jpg may be better.



来源:https://stackoverflow.com/questions/7677663/htaccess-image-to-web-page-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!