.htaccess image to web page redirect?

2020-05-09 00:32发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2020-05-09 01:10

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/
查看更多
甜甜的少女心
3楼-- · 2020-05-09 01:25

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

查看更多
登录 后发表回答