Skip to content

Instantly share code, notes, and snippets.

@1e0ng
Created October 8, 2015 07:01
Show Gist options
  • Save 1e0ng/f9e2196a412402d2c96d to your computer and use it in GitHub Desktop.
Save 1e0ng/f9e2196a412402d2c96d to your computer and use it in GitHub Desktop.
import re
FILE_PATH = 'nginx-rewrite.txt'
RR = 'RewriteRule'
def relate(r):
ans = re.findall(r'rewrite\s*([^\s]+?)\s*([^;\s]+?);', r)
assert len(ans) == 1
return ans[0]
def solve(l, rs):
rs = re.findall(r'[^\n]+', rs, re.I|re.M)
if l.startswith('='):
l = l[1:].strip()
assert len(rs) == 1
assert l[0] == '/'
f, t = relate(rs[0])
print RR + ' ^' + l[1:] + '$ ' + t + ' [QSA, L]'
else:
for r in rs:
f, t = relate(r)
assert f != '^(.*)$'
f = re.sub(r'^\^/', r'^', f)
print RR + ' ' + f + ' ' + t + ' [QSA, L]'
def main():
f = open(FILE_PATH).read()
for l in re.findall(r'location\s*([^{]+?)\s*{([^}]+)}', f, re.I|re.M):
solve(*l)
if __name__ == '__main__':
main()
@kakathu
Copy link

kakathu commented Dec 1, 2017

hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment