File: .htaccess

#----------------------------------------------------------------------------------------
# Main (site root) Apache config file.  This file overrides host defaults, and some 
# subfolders have their own .htaccess files that may in turn customize settings here.
#  
# In general, Apache settings are hierarchical.  At the top of the tree, the main 
# Apache config file (inaccessible on this site's current host) defines defaults and 
# allows/disallows per-folder .htaccess overrides.  Within a folder, settings are 
# inherited from all higher folders in the path, through the main Apache config file. 
# Hence, settings in a folder's .htaccess apply to that folder and all subfolders below 
# that level in the filesystem.  Because this file is highest, it's settings are 
# site-global.  For more: https://httpd.apache.org/docs/current/howto/htaccess.html.
#
# Coding note: comments in .htaccess files must generally be full-line only; see ahead.
#----------------------------------------------------------------------------------------


#----------------------------------------------------------------------------------------
# Jun-2017 redesign for programs, initial redirects 
#
# CAUTION: rewriting is ___iterative__:
# - it reruns all these in sequence again until no more changes, or default=10 times;
# - this means that a rewrite may be overwritten on the next iteration;
# - [L] just means end *this* iteration, and [END] may not be supported;
#----------------------------------------------------------------------------------------

rewriteengine on

# aug-2017: reroute _all_ /downloads*, including /downloads/, not just /downloads
rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^downloads(.*)$ "http\:\/\/learning-python\.com\/programs\.html" [R=301,L] #594463424e1e3

rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^recenthighlights(.*)$ "http\:\/\/learning-python\.com\/posts\.html" [R=301,L] #59446306007e6

rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^books(.*)$ "http\:\/\/learning-python\.com$1" [R=301,L] #59445ffbc6fdc


#----------------------------------------------------------------------------------------
# Jul-12-17: temp workaround for pyedit userguide broken product links (till next build): 
# present in app, exes, src.  This was also fixed in live unzipped copy, /Code, and 
# /Websites/{Programs, UNION}, but not in zipped products (will roll out in next build).
#----------------------------------------------------------------------------------------

rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^pyedit\.html\/PyEdit(.*).zip$ "http\:\/\/learning-python\.com\/pyedit-products\/PyEdit$1.zip" [R=301,L] #59664d12d58c0


#----------------------------------------------------------------------------------------
# Jul-12-17: temp workaround for mergeall userguide broken "Moredocs" bad case links 
# (till next build) [FOR LINUX ZIP ONLY].  This was also fixed in live unzipped copy,
# /Code, and /Websites/{Programs, UNION}, but not in zipped products.
#----------------------------------------------------------------------------------------

rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^mergeall-products\/unzipped\/docetc\/Moredocs(.*)$ "http\:\/\/learning-python\.com\/mergeall-products\/unzipped\/docetc\/MoreDocs$1" [R=301,L] #59664d12d58c0


#----------------------------------------------------------------------------------------
# Feb-2018: map all *.py not in cgi/ (at URL front) to the new showcode.py CGI script.
# Added for mobile redesign; an alternative to "add handler" or *many* manual link edits.
# This works so well that it was adopted for *.txt files too (per the generalized rule),
# such that even files in the unzipped product packages use the site-wide display style.
# This must avoid robot.txt files, and can impact autoindex README.txt files - see ahead.
#----------------------------------------------------------------------------------------

rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
rewritecond %{HTTP_HOST} ^learning-python.com$
rewriterule ^(?!(?:cgi\/|.*robots.txt))(.*).(py|txt|pyw)$ "http\:\/\/learning-python\.com\/cgi\/showcode\.py\?name\=$1.$2"

# before apr-23-18 excluding robots.txt (likely harmless)
# rewriterule ^(?!cgi\/)(.*).(py|txt|pyw)$ "http\:\/\/learning-python\.com\/cgi\/showcode\.py\?name\=$1.$2"

# this fails (loops) if listed first: why?
# rewriterule ^(.*)robots.txt$ "http\:\/\/learning-python\.com\/$1robots.txt" [L]


#----------------------------------------------------------------------------------------
# This never worked: its result was rewritten by the prior rule on the next iteration 
# (see above).  Instead, generate a plain-text reply in the cgi script itself, for the
# rawmode=view query param automatically sent by the formatted-display template's link.
#
# Feb-2018: but DO NOT route to script if it's a raw-text link in a reply page...
# rewritecond %{HTTP_HOST} ^www.learning-python.com$ [OR]
# rewritecond %{HTTP_HOST} ^learning-python.com$
# rewriterule ^cgi\/rawtext\/(.*)$ "http\:\/\/learning-python\.com\/$1"  [L] 
# [L] = skip run next rules if this fired, but on this iteration only...
# 
# RewriteCond %{ENV:REDIRECT_STATUS} !=200   # per a rumor, also failed
# RewriteRule .* -  [L]
#----------------------------------------------------------------------------------------



#----------------------------------------------------------------------------------------
# (Older terse comments) godaddy url redirects simply insert the above rewrite code
# these are just pcre (perl) REs [^=start after domain/, $=eostring, $1=(.*) group]
#
# Last line was this, which caused double // after domain (clear firefox network cache on updates):
# rewriterule ^books(.*)$ "http\:\/\/learning-python\.com\/$1" [R=301,L] #59445ffbc6fdc
#
# Tweaked 2nd rule, to be more inclusive; was:
# rewriterule ^recenthighlights\.html$ "http\:\/\/learning-python\.com\/posts\.html" [R=301,L] #59446306007e6
#
# This might work too (not verified)...
# RewriteCond %{THE_REQUEST} //
# RewriteRule .* $0 [R]
#----------------------------------------------------------------------------------------


#----------------------------------------------------------------------------------------
# Original .htaccess code (and less-polished docs)...
#
# Major changes:
#   Oct15: book site earthlink->godaddy relocation
#   Jun17: redesign for programs, union-folder model
#   Oct17: .htaccess fix for covert godaddy host move
#   Feb18: mobile-friendly redesign, showcode.py
#   Apr18: relocation to better server/host at godaddy
#   Jul18: autoindex READMEs broke again: revive _READMEs
#
# This is an Apache server config file: at this root level, 
# it provides defaults for the entire site - it's used for this 
# folder and all its subfolders, unless a subfolder has its own 
# .htaccess overrides.  The main Apache config file is maintained 
# by the hosting provider, and may vary per host (yes, argh).
#
# Here: generate file indexes for dirs, if no index.html; fix 
# autoindex name width to that of longest file name; view .py 
# (etc.) instead of running (change in cgi/).
#
# NOTE: at site, the top-level cgi/ script folder is actually 
# just a link to html/cgi, but it's special-cased so that 
# html/.htaccess (this flie) does not apply to its content;
# [Apr-2018: any folder can be used for cgi now.]
#
# Oct17 .htaccess fix:
# RemoveHandler was broken by godaddy relocating my site...
# Works only in root's .htaccess; had to comment out in all
# subdirs, but leave in root for .pys at the root level...
# permissions and content had no effect...
# all subfolder changed lines are "###RemoveHandler..."
# also requires "AddHandler cgi-script .py" in cgi/.htaccess...
#
# Feb18: added rewrite rule above for showcode.py file viewer;
# this broke README.txt files in auto-index pages (rewritten?);
# addressed by renaming to .html, <PRE> around text, and using
# "ReadmeName README.html" in subdirs .htaccess; this impacted
# 5 auto-index pages in /Code, plus 5 complete-app unzipped;
# later made auto-index .htaccess + README.html more complete;
# later copied README.txt to _README.txt so appear in listings;
# [Apr-18: even later... rules at new host differ; see ahead]
#
# TBD: should there be a "+" before NameWith? - as is, this 
# may be cancelling out some other IndexOption defaults (but 
# is unrelated to README.txt/.html autoindex: IndexIgnore);
# Apr-2018: adding a "+" seems to have no effect whatsoever...
#----------------------------------------------------------------------------------------

Options +Indexes
IndexOptions +NameWidth=*
RemoveHandler cgi-script .py .pyw .cgi


#----------------------------------------------------------------------------------------
# Apr-2018: on new cPanel web host at godaddy, the readme issue
# is different - autoindexes list both README.txt and _README.txt;
# display text of neither inline; and also list a README.html
# even if it's named as the ReadmeName file.  On the former host,
# README.txt was dropped altogether (not in list, and not inline)
# due to showcode rewrite rule above; hence the _README.txt copies.  
# The new host at least lists README.txt, but doesn't show it inline 
# despite the ReadmeName below.  Eventually drop _README*.txt copies; 
# exclude them here for now.  Perpetually-thrashing software is fun...
#
# COMMENTS in .htaccess files are full-line only - not to right of a
# directive (but some modules (e.g., IndexIgnore) allow this anyhow);
# for details see https://httpd.apache.org/docs/2.4/configuring.html 
#
# Comments of the following lines:
# 1) this is a no-op; why? (and a "#" on the left here fails)
# 2) prior host copies; kill these soon
# 3) assume it's the dir's ReadmeName
# 4) plus 9 more _README-*.txt files
#
# UPDATE, Jul-2018 => reinstate the _READMEs
# README.txt files have once again vanished from autoindex pages on 
# this site, due to an unknown godaddy apache-server change that was 
# both unannounced and beyond the hosting account's control (yes, blah); 
# neither activating the first line below nor eliminating README.txt 
# files in the showcode.py rewrite rule above had any effect; punt,
# but make+show the _README copy files again for at least some context;
#----------------------------------------------------------------------------------------

#ReadmeName README.txt
###IndexIgnore _README.txt
IndexIgnore README.html
###IndexIgnore _README?*.txt


#----------------------------------------------------------------------------------------
# Apr-2018: on new cPanel web host at godaddy, an access to "name"
# does not automatically return "name.html" unless the following is
# set (here, or in the main config file); examples: /training, /pyedit;
# avoid N rewrite rules; this also differs on this host/server only;
# See "apache content negotiation" for all the gory details.
#----------------------------------------------------------------------------------------

Options +MultiViews



[Home] Books Programs Blog Python Author Training Search Email ©M.Lutz