Last active
August 29, 2015 14:03
-
-
Save DrPaulBrewer/61b6ba76de31df22722c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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