In my root folder, the following captures https://example.org and http://example.org and redirects to its respective version (http or https) with the www added back in. (It's a requirement they had with an advertisement system.)
The problem however is when a subdir uses the "direct everything at the index.php" technique you see with MVC coding in PHP. It completely ignores the www rule.
My problem is that I need to make the subdirs, even if they have an .htaccess, to use the rule of the root .htaccess for the www redirect. What can I do to either ensure a subdir .htaccess applies the www rule from either the master, or at least edit the subdir .htaccess ?
# ROOT .htaccess FILE
RewriteEngine on
# IF HTTPS OFF AND NO WWW, REDIRECT TO HTTP://WWW.*
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
# IF HTTPS ON AND NO WWW, REDIRECT TO HTTPS://WWW.*
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
The one in some few subdirs looks like:
# SUBDIR .htaccess FILE
Options All +FollowSymLinks -Indexes -Multiviews
# BLOCK ROBOTS AND SPIDERS FROM SCANNING CERTAIN FILES
IndexIgnore .htaccess *~ *.txt *.sql *.xml
# BLOCK SOME COMMON FILE TYPES FROM END USERS
<FilesMatch "\.(sql|xml|txt|htaccess)">
Deny from all
</FilesMatch>
<FilesMatch ".*~$">
Deny from all
</FilesMatch>
RewriteEngine On
# BESIDES BLOCKED FILES ABOVE, OR FILES WE PHYSICALLY HAVE IN THIS DIRECTORY,
# REDIRECT ALL OTHER OUTPUT TO index.php AND PASS ALL URL PARAMETERS IN FULL.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [QSA]
Note that I tried something like this in the subdir .htaccess files, but it failed -- I ended up with 404s in the subdir paths.
# SUBDIR .htaccess FILE
Options All +FollowSymLinks -Indexes -Multiviews
# BLOCK ROBOTS AND SPIDERS FROM SCANNING CERTAIN FILES
IndexIgnore .htaccess *~ *.txt *.sql *.xml
# BLOCK SOME COMMON FILE TYPES FROM END USERS
<FilesMatch "\.(sql|xml|txt|htaccess)">
Deny from all
</FilesMatch>
<FilesMatch ".*~$">
Deny from all
</FilesMatch>
RewriteEngine On
# IF HTTPS OFF AND NO WWW, REDIRECT TO HTTP://WWW.*
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
# IF HTTPS ON AND NO WWW, REDIRECT TO HTTPS://WWW.*
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
# BESIDES BLOCKED FILES ABOVE, OR FILES WE PHYSICALLY HAVE IN THIS DIRECTORY,
# REDIRECT ALL OTHER OUTPUT TO index.php AND PASS ALL URL PARAMETERS IN FULL.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [QSA]
EDIT:
For example...
I want to go to:
1 http://example.org/
2 http://www.example.org/
3 https://example.org/
4 https://www.example.org/
5 http://example.org/test/something.php
6 http://example.org/test/
7 https://www.example.org/test/
8 https://www.example.org/test/update-user
...and the test folder is a PHP MVC platform so it uses the "direct everything at index.php" technique inside that folder.
...and so I want the above to translate to:
1 http://www.example.org/
2 http://www.example.org/
3 https://www.example.org/
4 https://www.example.org/
5 http://www.example.org/test/something.php
6 http://www.example.org/test/
7 https://www.example.org/test/
8 https://www.example.org/test/update-user
It's the "www. subdomain no matter what" strategy. And note that it kept the https intact if that was used.
The trouble is that the test folder's .htaccess file ignores what's in the root .htaccess file, and I don't know how to edit the test folder's .htaccess file such that it can do the www. thing properly. I attempted it (as I demonstrated above in the third code sample) and I ended up with 404 errors.