Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DrPaulBrewer
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrPaulBrewer/61b6ba76de31df22722c to your computer and use it in GitHub Desktop.
Save DrPaulBrewer/61b6ba76de31df22722c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Copyright 2014 Dr Paul Brewer
# This python file is licensed to the public under the terms of the MIT License
# -----
# When redirecting from name.com to www.name.com or from www.name.com to name.com
# it is easy to write the NGINX response incorrectly as
# return 301 http://name.com/$request_uri
# when it should be
# return 301 http://name.com$request_uri
# For more info see http://stackoverflow.com/a/7958540/103081
# The below code automated fixing this for some sites I own
import os
def fix(fname):
outfile = open(fname+".fixed","w")
with open(fname,"r") as infile:
for inline in infile:
outline = ''
if inline.find(r'return 301')>=0:
outline = inline.replace(r'.com/$request',r'.com$request')
else:
outline = inline
outfile.write(outline)
outfile.close()
os.rename(fname+".fixed",fname)
fnames = os.listdir(".")
for fname in fnames:
if fname.find(".git")==-1:
fix(fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment