【问题标题】:.htaccess rewrite rule with regex not working, returns 404.htaccess 用正则表达式重写规则不起作用,返回 404
【发布时间】:2015-12-08 20:14:09
【问题描述】:

我尝试使用 timthumb 库调整图像大小。
我可以通过以下网址直接使用

http://localhost/study/server/resize/thumb.php?src=http://localhost/study/server/product/1/1/orig-1.jpg&w=160&h=130

而且效果很好。

但是当我像这样重写我的网址时

http://localhost/study/server/resize/160x130/r/product/1/1/orig-1.jpg

并添加

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(resize) [NC]
    RewriteRule ^resize/(.*)x(.*)/r/(.*) resize/thumb.php?src=http://localhost/study/server/$3&h=$2&w=$1&c=1
</IfModule>

不幸的是,这条规则返回 404 错误

【问题讨论】:

  • 什么服务器可以使用 wamp 或 xammp...?
  • ubuntu apache2 httpd

标签: php .htaccess url-rewriting


【解决方案1】:

使用这个RewriteRule

RewriteRule ^study/server/resize/([0-9]+)x([0-9]+)/r/product/([0-9]+)/([0-9]+)/(.*)$ 
/study/server/resize/thumb.php?src=/study/server/product/$3/$4/$5&w=$1&h=$2 [L]

group 1 匹配宽度,零个或多个数字
group 2 匹配高度,零个或多个数字
group 3 匹配 /product/ 后的第一位,一位,可根据需要修改
group 4 匹配 /product/ 后的第二位,一位,可根据需要修改
组 5 匹配文件名

这个条件

RewriteCond %{REQUEST_URI} !^/(resize) [NC]

不需要

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多