Forum Moderators: phranque
# Rewrite to HTTPS for secure 'other'.html pages in /private/admin/test/This produced the '301' status code correctly. From the 'Live HTTP Headers' result I quoted earlier (#4672968 at 8:30 am on May 21, 2014), it appeared there was a call for authentication. But there was not an authentication pop-up window delivered to the browser; I don't know why.
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^((?:characters|course-data-read|links-index)\.html)$ [example.com...] [R=301,L]
Options +FollowSymlinksI realise there are parts here that could be expressed more succinctly with better regular expressions, but I thought it best to get the functionality correct before fine-tuning. Excepting for the addition of the '404' file, the file and directory structure is still as I listed earlier.
Options -Indexes
<Files "robots.txt">
Order Allow,Deny
Allow from all
</Files>
Addhandler application/x-httpd-php5 .html .php
ErrorDocument 404 http://example.com/art/404-not-found.html
RewriteEngine on
RewriteBase /
# (1) Block useless bots
RewriteCond %{HTTP_USER_AGENT} (Baiduspider|YandexBot) [NC]
RewriteRule . - [F]
# (2) Rewrite those seeking 'als' to 'afa', 'bookings1' to 'bookings2', 'sf1' to 'pf1', 'sf2' to 'pf2', and 'sfa' to 'pf2'
RewriteRule ^als(.*)$ http://example.com/afa$1 [R=301,L]
RewriteRule ^bookings1(.*)$ [example.com...] [R=301,L]
RewriteRule ^sf([12].*)$ http://example.com/pf$1 [R=301,L]
RewriteRule ^sfa(.*)$ http://example.com/pf2$1 [R=301,L]
# (3) Rewrite to HTTP for non-secure 'other'.html pages at the root
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^((?:tour|example[-a-z]+)\.html)$ http://example.com/$1 [R=301,L]
# (4) Rewrite to HTTP for non-secure 'other'.html pages in /art/
# RewriteCond %{HTTPS} =on [OR]
# RewriteCond %{SERVER_PORT} 443
# RewriteRule ^((?:banner|buttons-row[123]|contacts)\.html)$ http://example.com/art/$1 [R=301,L]
# (5) Rewrite to HTTPS for secure 'other'.html pages in /bookings2/
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^((?:booking-entry|booking-save)\.html)$ [example.com...] [R=301,L]
# (6) Rewrite to HTTPS for secure identity.html page in /private/history/
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^identity\.html$ [example.com...] [R=301,L]
# (7) Rewrite to HTTPS for secure bookmarks.html page in /private/reference/
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^bookmarks\.html$ [example.com...] [R=301,L]
# (8) Rewrite to HTTPS for secure buttons-row4.html page in /private/admin/
# RewriteCond %{HTTPS} =off [OR]
# RewriteCond %{SERVER_PORT} !443
# RewriteRule ^buttons-row4\.html$ [example.com...] [R=301,L]
# (9) Rewrite to HTTPS for secure 'other'.html pages in /private/admin/test/
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^((?:characters|course-data-read|links-index)\.html)$ [example.com...] [R=301,L]
# (10) Rewrite to HTTP for non-secure 'other'.html pages
# RewriteCond %{HTTPS} =on [OR]
# RewriteCond %{SERVER_PORT} 443
# RewriteRule ^((?:art)/.*)?$ http://example.com/$1 [R=301,L]
# (11) Rewrite to HTTPS for secure 'other'.html pages
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^((?:bookings2|private/history|private/reference|private/admin|private/admin/test).*) [example.com...] [R=301,L]
# (12) Rewrite to HTTP for non-secure index.html pages
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^((afa|als|art|bookings|classes|els|faqs|fees|forms|gallery|pf1|pf2|rfa|unused)/)?index\.html http://example.com/$1 [R=301,L,NS]
# (13) Rewrite to HTTPS for secure index.html pages
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^((admin|bookings2|private|private/history|private/reference|private/admin|private/admin/counter|private/admin/file-log|private/admin/files|private/admin/page-log|private/admin/test|private/trainers)/)index\.html [example.com...] [R=301,L,NS]
# (14) Host Name Canonicalisation (use non-www form of new domain)
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
almost, but not totally, unnecessary
ErrorDocument 404 http://example.com/art/404-not-found.html
ErrorDocument 404 /art/404-not-found.html # (1) Block useless bots
...
RewriteRule . - [F]
# (2) Rewrite ...
# (5) Rewrite to HTTPS for secure 'other'.html pages in /bookings2/
...
RewriteRule ^((?:booking-entry|booking-save)\.html)$ [example.com...] [R=301,L]
# (10) Rewrite to HTTP for non-secure 'other'.html pages
...
# RewriteRule ^((?:art)/.*)?$
# (12) Rewrite to HTTP for non-secure index.html pages
<snip>
# (13) Rewrite to HTTPS for secure index.html pages
This produced the '301' status code correctly. From the 'Live HTTP Headers' result I quoted earlier (#4672968 at 8:30 am on May 21, 2014), it appeared there was a call for authentication. But there was not an authentication pop-up window delivered to the browser; I don't know why.
HTTP/1.1 301 Moved Permanently
...
WWW-Authenticate: Basic realm="Example - Members Only"
...
Location: http://example.com/401.shtml
Options +FollowSymlinks
Options -Indexes
<Files "robots.txt">
Order Allow,Deny
Allow from all
</Files>
Addhandler application/x-httpd-php5 .html .php
ErrorDocument 401 /art/401-unauthorised.html
ErrorDocument 404 /art/404-not-found.html
RewriteEngine on
# (1) Block useless bots
RewriteCond %{HTTP_USER_AGENT} (Baiduspider|YandexBot) [NC]
RewriteRule .? - [F]
# (2) Redirect those seeking superseded pages to their replacements:
# 'als' to 'afa', 'bookings1' to 'bookings2', 'sf1' to 'pf1', 'sf2' to 'pf2', and 'sfa' to 'pf2'
RewriteRule ^als(.*)$ [example.com...] [R=301,L]
RewriteRule ^bookings1(.*)$ [example.com...] [R=301,L]
RewriteRule ^sf([12].*)$ [example.com...] [R=301,L]
RewriteRule ^sfa(.*)$ [example.com...] [R=301,L]
# (3) Redirect requests for index.html to show the path without the file name
RewriteRule ^(.*)index\.html$ [example.com...] [R=301,L]
# (4) Host Name Canonicalisation (use non-www form of secure domain)
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443 [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ [example.com...] [R=301,L]
http://www.old-example.com/privateie. wrong protocol, wrong domain, and requiring authorisation, I am served the page 'https://example.com/art/401-unauthorised.html' straight away. Obviously, the correct protocol and domain have been served. Looking at the Live HTTP Headers, I can see that the need for authorisation has been recognised, but it has not been requested. Even if I previously make an accurately addressed request, and enter username and password, making that wide-of-the-mark request in the same browser session does not make use of the cached authorisation: the 401 error still results.
AuthType Basic
AuthName "Private - Members Only"
AuthUserFile /home/example/.htpasswds/.htpasswd
AuthGroupFile /dev/null
Require valid-user
RewriteRule ^(.*)index\.html$ [example.com...] [R=301,L]
RewriteRule ^(.*[^/])/?index\.html$ [example.com...] [R=301,L]
Logically, I would have thought my request would go to the .htaccess in the 'public_html' directory
I would have preferred the '/afa/index.html' page, for example, to be served as ' [example.com...]
Careful. You're mixing two different processes. Normally, "index.html" only applies to actual, physical directories. But if your code uses URLs like/afa
then you're talking about extensionless URLs, where/afa
is quietly rewritten to/afa.php
(or html or whatever it really is).
Question for people who know search-engine behavior better than I do: If requests forafa.html
are redirected toafa
will the search engine ask forafa/
and henceafa/index.html
? You only need rules for requests that actually occur.[edited by: Ocean10000 at 10:50 pm (utc) on Jul 27, 2014]
[edit reason] Fixed Requested Mispelling. [/edit]
Before anything else: Does the inner htaccess contain the line
RewriteEngine On
without the line
RewriteOptions inherit
? If yes to both, then all earlier RewriteRules will be abandoned as if they had never existed.
AuthType Basic
AuthName "Private - Members Only"
AuthUserFile /home/example/.htpasswds/.htpasswd
AuthGroupFile /dev/null
Require valid-user
http://www.old-example.com/private
GET /private HTTP/1.1
Host: www.old-example.com
...
HTTP/1.1 301 Moved Permanently
Date: Mon, 28 Jul 2014 00:43:38 GMT
Server: Apache
WWW-Authenticate: Basic realm="Private - Members Only"
Location: [example.com...]
...
----------------------------------------------------------
[example.com...]
GET /art/401-unauthorised.html HTTP/1.1
Host: example.com
...
HTTP/1.1 200 OK
Date: Mon, 28 Jul 2014 00:43:38 GMT
Server: Apache
...
----------------------------------------------------------
...
AuthType Basic
AuthName "Private - Members Only"
AuthUserFile /home/example/.htpasswds/.htpasswd
AuthGroupFile /dev/null
Require valid-user
RewriteEngine on
RewriteOptions inherit
RewriteRule ^(.*)index\.html$ [example.com...] [R=301,L]
http://httpd.apache.org/docs/current/mod/mod_dir.htmlshows the issues and possible security pitfalls of deleting trailing slashes for directories.
Options +FollowSymlinks
Options -Indexes
ErrorDocument 401 /art/401-unauthorised.html
ErrorDocument 404 /art/404-not-found.html
RewriteEngine On
# Redirect requests for index.html to show the path without the file name
RewriteRule ^(.*)index\.html$ [example.com...] [R=301,L]
# Host Name Canonicalisation (use non-www form of domain)
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{SERVER_PORT} !443 [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ [example.com...] [R=301,L]
AuthType Basic
AuthName "Private - Members Only"
AuthUserFile /home/example/.htpasswds/.htpasswd
AuthGroupFile /dev/null
Require valid-user
http://www.oldexample.com/privatethe 401 page is served:
https://example.com/art/401-unauthorised.html
http://www.oldexample.com/private
GET /private HTTP/1.1
Host: www.oldexample.com
...
HTTP/1.1 301 Moved Permanently
Date: Mon, 04 Aug 2014 01:41:04 GMT
Server: Apache
WWW-Authenticate: Basic realm="Private - Members Only"
Location: [example.com...]
...
----------------------------------------------------------
[example.com...]
GET /art/401-unauthorised.html HTTP/1.1
Host: example.com
...
HTTP/1.1 200 OK
Date: Mon, 04 Aug 2014 01:41:05 GMT
Server: Apache
...
----------------------------------------------------------
...
#request# GET [oldexample.com...]
GET /private
#request# GET [example.com...]
#redirect# GET /art/401-unauthorised.html
...
RewriteEngine Onor
RewriteOptions Inherit
RewriteEngine Onto the subdirectory .htaccess file does not seem to fix the behaviour.
RewriteOptions InheritBefore