【问题标题】:Redirect file type specific requests重定向文件类型特定请求
【发布时间】:2013-10-04 18:57:15
【问题描述】:

在我的应用程序中,我有许多这样的图像的 src:

路径/其他路径/my-image.jpg

这可能会很长,并且可能会将我的一些文件结构暴露给外界。

我可以在 .htaccess 中做什么来将给定的文件扩展名重定向到目录?

即我的图像 src 只是“my-image.jpg”,.htaccess 看到这是一个 .jpg 文件并重定向到文件夹“www.site.com/my-jpgs/”是“my-image.jpg”所在的文件夹,因此加载图像。

这是我目前拥有的:

    Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$

RewriteRule ^(.*)$ public_html/index.php

RewriteBase /

# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f 
# Only use the filename and extension.
RewriteRule (.[^/]*)\.(gif|jpg|png) public_html/images$1.$2 [R,L]

我已经让它工作了,除了浏览器发出两个看起来像这样的请求:

请求网址:http://www.view.com/activity/enquiry/dropdown-btn.gif

请求网址:http://www.view.com/public_html/images/dropdown-btn.gif

第二个是正确的,我该如何更正它才不会发出第一个不正确的请求?

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    重写规则的顺序在.htaccess. Your problem is incorrect ordering of your rule. You need to move last rule to the top just belowRewriteEngine Online to have external redirect before sending everything off toindex.php`中非常重要:

    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine On
    RewriteBase /
    
    # Ignore rule if the real path exists, use that instead.
    RewriteCond %{REQUEST_FILENAME} !-f 
    # Only use the filename and extension.
    RewriteRule ^(?:[^/]+/)*([^.]+\.(?:gif|jpe?g|png))$ images/$1 [R,L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/importers
    RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$
    RewriteRule ^ index.php [L]
    

    【讨论】:

    • 谢谢,但它仍然请求它两次。
    • 您在浏览器中输入了什么网址?
    • 我所在的网址是 ht tp://www.site.com/activity/enquiry/50647 GIF 请求在我的编辑中。
    • 你的意思是这个gif请求Request URL:http://www.view.com/activity/enquiry/dropdown-btn.gif
    • 是的,这是错误的请求。那基本上是把gif作为参数发送给类活动的方法查询,这是完全错误的。
    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    相关资源
    最近更新 更多