重写包含“?”的html-Image路径通过mod_rewrite

时间:2013-08-13 15:17:35

标签: .htaccess mod-rewrite

我已经搜索了几个小时的教程,以了解如何重写包含“?”的图像路径网址。

我的图片如下所示:

<img src="/index.php?rex_resize=600w__/imageName.jpg" >

为了缓存所有图像,我必须/想要删除这个“?”来自字符串。

这是我使用的规则:

RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]

这是我的.htaccess中包含我的rewriteRule的所有内容:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]

RewriteRule ^sitemap\.xml$ index.php?rexseo_func=googlesitemap [NC,L]
RewriteRule ^robots\.txt$ index.php?rexseo_func=robots [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^redaxo/.*
RewriteCond %{REQUEST_URI} !^files/.*
RewriteCond %{REQUEST_URI} !^google(.*).html*
RewriteRule ^((.|\r|\n)+)/? index.php?params=$1 [L,NC]
</IfModule>

但是因为我从未学过适当的RewriteRules,所以我从RewriteRule中得不到匹配,而且我对此的了解非常有限。

对于一些帮助,我很感激:)

1 个答案:

答案 0 :(得分:0)

您需要在网址中找出要匹配的模式,以便知道必须重写当前请求到您的图片脚本。

根据您的帖子和内容,让我们说模式是:

所有URI ...

  • files /
  • 开头
  • 以网络图片后缀结尾( gif jpg png
  • 之间的

将其写入正则表达式并完成:

  • / :开始使用URI
  • files / :我们的假目录
  • (。*):捕获任何内容(将以1美元存储)
  • \。:一个点
  • (gif | jpg | jpeg | png):文件后缀(将以2美元存储)
  • $ :URI结尾

RewriteRule:

RewriteRule ^/files\/(.*)\.(gif|jpg|jpeg|png)$ index.php?rex_resize=$1.$2 [NC,L]

示例:

传入请求:

/files/600w/imageName.jpg

Mod_Rewrite之后的请求:

/index.php?rex_resize=600w/imageName.jpg

请注意,您的示例显示了重定向到/index.php(以斜杠开头),当您使用.htaccess文件时它不起作用(只有在whost配置中有规则时才会这样做)

相关问题