Apache的mod_rewrite复杂的URL正则表达式 [英] Apache mod_rewrite complex URL regex

查看:206
本文介绍了Apache的mod_rewrite复杂的URL正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这样的URL做出漂亮的网址:

<$c$c>http://example1.com/cimage/webroot/img.php?src=http://example.com/img1.jpg&w=600&h=800&q=60&sharpen&crop-to-fit

所以结果URL必须是财产以后这样的:

<$p$p><$c$c>http://example1.com/cimage/webroot/img.php/http://example2.com/img1.jpg/600*800/60/sharpen/crop-to-fit

现在我的问题创建在Apache的mod_rewrite使用正则表达式。
请帮帮我。谢谢...


解决方案

根据在评论的讨论,这是解决方案:

 重写规则^ img.php /(HTTPS):/ +(+(:??\\ JPG | \\巴纽))/(\\ d +)/(\\ D + )/(\\ d +)/([^ \\ /] *)/([^ \\ /] *)/([^ \\ /] *)$ img.php SRC = $ 1:????// $ 2 &安培; W = $ 3及H = $ 4安培; q = $ 5和$ 6安培; $ 7安培; $ 8个

演示

这里的逻辑是在字符串的开始,那么无论 HTTP 或匹配 img.php / HTTPS ,一个或多个 / s时,主机和文件名,然后将不同的参数,尺寸,质量等。

来处理这个没有复杂的正则表达式的另一种方法是做这样一个简单的包罗万象的规则:

 重写规则^ img.php \\ /(+)img.php?URL = $ 1

然后,你可以做解析在PHP中,大都采用如简单的操作爆炸()在PHP code。这种做法是有道理的,特别是如果有可能是额外的操作/参数;否则,你的正则表达式开始有一个的很多的捕获组,使其难以阅读和维护的。

i want to make a beautiful URL from a URL like this:

http://example1.com/cimage/webroot/img.php?src=http://example.com/img1.jpg&w=600&h=800&q=60&sharpen&crop-to-fit

So the result URL must be somthing like this:

http://example1.com/cimage/webroot/img.php/http://example2.com/img1.jpg/600*800/60/sharpen/crop-to-fit

Now my problem create a regex for use in apache mod_rewrite. help me please. thanks...

解决方案

Based on the discussion in the comments, this is the solution:

RewriteRule ^img.php/(https?):/+(.+?(?:\.jpg|\.png))/(\d+)/(\d+)/(\d+)/?([^\/]*)/?([^\/]*)/?‌​([^\/]*)$ img.php?src=$1://$2&w=$3&h=$4&q=$5&$6&$7&$8

Demo

The logic here is to match img.php/ at the start of the string, then either http or https, :, one or more /s, the host and file name, then the various parameters for size, quality, etc.

Another way to handle this without the complicated regex is to do a simple catch-all rule like this:

RewriteRule ^img.php\/(.+) img.php?url=$1

Then you can do the parsing in PHP, mostly using simpler operations like explode() in the PHP code. This approach makes sense especially if there might be additional operations/parameters; otherwise, your regex starts to have a lot of capturing groups, making it hard to read and maintain.

这篇关于Apache的mod_rewrite复杂的URL正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆