Apache mod_rewrite 复杂 URL 正则表达式
Apache mod_rewrite complex URL regex
我想用这样的 URL 做一个漂亮的 URL:
http://example1.com/cimage/webroot/img.php?src=http://example.com/img1.jpg&w=600&h=800&q=60&sharpen&crop-to-fit
所以结果 URL 一定是这样的:
http://example1.com/cimage/webroot/img.php/http://example2.com/img1.jpg/600*800/60/sharpen/crop-to-fit
现在我的问题是创建一个在 apache 中使用的正则表达式 mod_rewrite。
请帮帮我。谢谢...
根据评论中的讨论,这是解决方案:
RewriteRule ^img.php/(https?):/+(.+?(?:\.jpg|\.png))/(\d+)/(\d+)/(\d+)/?([^\/]*)/?([^\/]*)/?([^\/]*)$ img.php?src=://&w=&h=&q=&&&
这里的逻辑是匹配字符串开头的img.php/
,然后是http
或者https
,:
,一个或者多个/
s,主机名和文件名,然后是大小、质量等各种参数。
另一种不用复杂的正则表达式来处理这个问题的方法是像这样做一个简单的包罗万象的规则:
RewriteRule ^img.php\/(.+) img.php?url=
然后您可以在 PHP 中进行解析,主要使用更简单的操作,例如 PHP 代码中的 explode()
。这种方法很有意义,特别是如果可能有额外的 operations/parameters;否则,您的正则表达式开始有 lot 个捕获组,使其难以阅读和维护。
我想用这样的 URL 做一个漂亮的 URL:
http://example1.com/cimage/webroot/img.php?src=http://example.com/img1.jpg&w=600&h=800&q=60&sharpen&crop-to-fit
所以结果 URL 一定是这样的:
http://example1.com/cimage/webroot/img.php/http://example2.com/img1.jpg/600*800/60/sharpen/crop-to-fit
现在我的问题是创建一个在 apache 中使用的正则表达式 mod_rewrite。 请帮帮我。谢谢...
根据评论中的讨论,这是解决方案:
RewriteRule ^img.php/(https?):/+(.+?(?:\.jpg|\.png))/(\d+)/(\d+)/(\d+)/?([^\/]*)/?([^\/]*)/?([^\/]*)$ img.php?src=://&w=&h=&q=&&&
这里的逻辑是匹配字符串开头的img.php/
,然后是http
或者https
,:
,一个或者多个/
s,主机名和文件名,然后是大小、质量等各种参数。
另一种不用复杂的正则表达式来处理这个问题的方法是像这样做一个简单的包罗万象的规则:
RewriteRule ^img.php\/(.+) img.php?url=
然后您可以在 PHP 中进行解析,主要使用更简单的操作,例如 PHP 代码中的 explode()
。这种方法很有意义,特别是如果可能有额外的 operations/parameters;否则,您的正则表达式开始有 lot 个捕获组,使其难以阅读和维护。