File: .htaccess

#----------------------------------------------------------------------------------------
# Main (site root) Apache config file.  Some folders customize some settings here.
# Settings are inherited up all folders in path, through the site's main config file,
# which defines defaults and allows/disallows .htaccess overrides.  Hence, settings
# in a folder's .htaccess apply to that folder and all subfolders below that level.
# See https://httpd.apache.org/docs/current/howto/htaccess.html for more details.
# Comments in .htaccess files must generally be full-line only; see ahead.
#----------------------------------------------------------------------------------------


#----------------------------------------------------------------------------------------
# Jun-2017 redesign for programs, initial redirects 
#
# CAUTION: rewriting is -iterative-:
# reruns all these in sequence again until no changes, or default=10 times;
# this means 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 updates;
# 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-side display style;
#----------------------------------------------------------------------------------------

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 prior rule on next iteration (see above);
# instead, pump out plain text in the cgi script itself, for template's raw=yes query param
#
# 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 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]
#----------------------------------------------------------------------------------------


#----------------------------------------------------------------------------------------
# Orignal .htaccess code (and 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 new server/host at godaddy
#
# 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 autondex name width to that of longest file name;
# view .py (etc.) instead of running (change in cgi/).
#
# NOTE: at site, top-level cgi/ is actually just
# a link to html/cgi, but it's special-cased so
# that html/.htaccess (this) does not apply
# [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 anyow);
# 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 "#" here fails)
# 2) prior host copies; kill these soon
# 3) assume it's the dir's ReadmeName
# 4) plus 9 more _README-*.txt files
#----------------------------------------------------------------------------------------

#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