【问题标题】:Redirect image url to another page using .htaccess使用 .htaccess 将图像 url 重定向到另一个页面
【发布时间】:2019-08-14 11:36:50
【问题描述】:

我想重定向这个网址

http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg

主网址如下。

http://192.168.1.101/project/test/wordpress/redirect.php?file=http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg

如果 url 包含 .jpg、.png、.jpeg、.bmp、.png 等扩展名,则将其全部移至 redirect.php,文件名如下。

http://192.168.1.101/project/test/wordpress/redirect.php?file=imagefile

我当前的 .htaccess 代码

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/test/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/test/wordpress/index.php [L]
</IfModule>
# END WordPress

请帮帮我..

【问题讨论】:

    标签: image .htaccess redirect


    【解决方案1】:

    RewriteBase 行之后插入这一行:

    RewriteRule ^wp-content/.+?\.(jpe?g|png|gif|bmp)$ redirect.php?file=http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L,NC]
    

    一旦您确认它工作正常,请将R=302 替换为R=301。在测试你的 mod_rewrite 规则时避免使用R=301(永久重定向)。

    【讨论】:

    • 此 URL 在浏览器中是否有效:http://192.168.1.101/project/test/wordpress/redirect.php?file=imagefile ?
    • 这很适合我在本地开发站点上查看实时站点图像,而无需下载整个 /uploads/ 目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2014-08-10
    • 2013-08-04
    • 2014-09-01
    • 2016-12-19
    相关资源
    最近更新 更多