math with regexp in mod_rewrite

痞子三分冷 提交于 2019-12-22 23:47:13

问题


I'm trying to do the following:

I have the url: http://www.example.com/9999
and I want it to subtract 5000 from that number, and seamlessly redirect to: http://www.example.com/page.php?id=4999

Can regular expressions in mod_rewrite do maths?


回答1:


No. There is no math in regexp.




回答2:


There might not be math in regexp, but what I needed was perfectly doable:

Options +FollowSymLinks  
Options +Indexes  
RewriteEngine On
RewriteRule ^5([0-9][0-9][0-9])($) /offer_details.php?offers_id=$1$2 [R]
RewriteRule ^6([0-9][0-9][0-9])($) /offer_details.php?offers_id=1$1$2 [R]
RewriteRule ^7([0-9][0-9][0-9])($) /offer_details.php?offers_id=2$1$2 [R]
RewriteRule ^8([0-9][0-9][0-9])($) /offer_details.php?offers_id=3$1$2 [R]
RewriteRule ^9([0-9][0-9][0-9])($) /offer_details.php?offers_id=4$1$2 [R]

Out product IDs started from 5000, and we recently passed the 5000 mark (offer ID 10000+), so I'm gonna have to add, like, ten lines for 10000+, too. And by the time we reach offer id 20000 (>5 years) there would hopefully be technology to save me the writing of another 10 lines. Something along the lines of a nuclear apocalypse will do.




回答3:


Make a conditional loop with the number of lines, and set the number of lines to increment up by one whenever the threshhold is reached for lines of 10, the 100th line would be generated when the 999th element appears.



来源:https://stackoverflow.com/questions/4953923/math-with-regexp-in-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!