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

后端 未结 1 1993
旧巷少年郎
旧巷少年郎 2021-01-24 12:38

I want to redirect

www.domain.com/images/Apple.jpg


to

www.domain.com/Apple.html

相关标签:
1条回答
  • 2021-01-24 13:14

    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.

    0 讨论(0)
提交回复
热议问题