.htaccess , How to redirect any .jpg page to its attachment page (HTML)

…衆ロ難τιáo~ 提交于 2019-12-31 04:05:06

问题


I want to redirect

www.domain.com/images/Apple.jpg


to

www.domain.com/Apple.html

Here is the Live working example, This is what i exactly want
http://more-sky.com/data/out/2/IMG_21306.jpg

How it redirect to its parent page i.e (http://more-sky.com/WDF-21306.html)


回答1:


Assuming you have mod_rewrite enabled in your Apache:

RewriteEngine On
RewriteBase /
RewriteRule ^images/([^\.]+)\.(jpe?g|png) /$1.html  [QSA,L]

Edit: as noticed by Wh1T3h4Ck5, QSA (Query String Append) flag would be optional, as well as L (Last) would depend on the context and other rules present. Please, read about RewriteRule flags for more information.


To clarify: this solution assumes that an HTML document with the name of the image already exists. Something like:

<html>
    <head>
        <title>Apple</title>
    </head>
    <body>
        <img src="images/Apple.jpg" alt="Apple">
    </body>
</html>

This is not an optimal solution for a large number of images, but it seemed to fit OP's case.



来源:https://stackoverflow.com/questions/41344444/htaccess-how-to-redirect-any-jpg-page-to-its-attachment-page-html

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