Here are some frequently asked questions about Apache WebServers.

How do I force all requests to use HTTPS ?

After you add an SSL Certificate to your website, people may still try to connect to it using HTTP, which is considered insecure these days.
To address this, you can either create a separate directory for “secure” pages or simply redirect pages to always use HTTPS. If you are using an Apache webserver.
Add the following to the beginning of your .htaccess file.

#BEGIN HTTPS Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
#END HTTPS Redirect

This essentially says:

  • Is the Apache rewrite module loaded?
  • If so, then turn it on.
  • Is this an unsecured request (URL) that was sent?
  • Ok, then rebuild the whole URL to put https:// in the front of the URL to make it a secure request.
  • When done, Redirect to the new URL and mark this as a permanent redirect [R=301]
    Note. You can also redirect with just R or R=302. The 302 says this is a temporary redirect.
    Often webservers will remember permanent redirects and will cache them for speed.
  • Leave .htaccess and go no further. [L]
  • The whole process will start over from scratch again. However, this time through it will be https://…

Note. Thank you Jacob Buller of EcoVillages.CA for your help in debugging this.

Why do I keep getting “too many redirects” when I retrieve the Home Page, but not any other page ?

This can be caused by a number of things, however, if you use more than one “environment” under your registered website name, this solution should fix this for you. Also, if you have recently switched from Plain PermaLinks to %postname% PermaLinks or others, you may run into this problem as well.

 

By “environments”, I mean things like test, devl, next (stage), prod, etc. These are normally called sub-domains. They could also be different language translations. EG. en, fr, es, ru, etc. Regardless, the situation results in too many redirects as WordPress does not automatically redirect to index.php when the URL ends with a directory name with or without a trailing /, resulting in an infinite loop.

 

For example: If you have registered MySite.com, the internet will direct your request to where your main site is located. EG. https://MySite.com. You may have two environments, stage and prod, which is quite common and strongly recommended. Stage is where you make your changes and test them, prod is your environment that the world sees.


Hence you have two WordPress installations: https://MySite.com/stage and https://MySite/prod. You normally want prod to be your “goto” WordPress site, so you put a redirect into the MySite.com “home” directory to send people automatically to prod. For example, you would create an index.php file in your home directory which contains the following redirect. (Note. The home directory is usually public_html in your cPanel File Manager.)

<?php
/* Redirect to MySite.com/prod with a permanent redirect */
header( ‘Location: https://MySite.com/prod/’, TRUE, 301 );
?>

You can access the other directories using the URL request. EG. https://MySite.com/stage. Generally, this will partially work if you include a page name. EG. https://MySite.com/stage/about_us, however, the directives that WordPress inserts by default, break when no page name is specified. EG. https://MySite.com/stage or https://MySite/stage/ and https://MySite.com/prod or https://MySite.com/prod and since you have the redirect in the home directory, https://MySite.com or https://MySite.com/.


Using our example, you need to add the following to the beginning of the .htaccess file in public_html/stage.

# BEGIN Aldersoft Permalink fix
# Fix WordPress Permalink bug for URLS, not including index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule .* – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^$ https://%{HTTP_HOST}/stage/index.php [R,L]
RewriteRule ^(.*)%{HTTP_HOST}$ $1%{HTTP_HOST}/stage/index.php [R,L]
RewriteRule ^(.*)%{HTTP_HOST}/$ $1%{HTTP_HOST}/stage/index.php [R,L]
</IfModule>
#END Aldersoft Permalink Fix

and also the following needs to be added to the beginning of your .htaccess in public_html/prod.

# BEGIN Aldersoft Permalink fix
# Fix WordPress Permalink bug for URLS, not including index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule .* – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^$ https://%{HTTP_HOST}/prod/index.php [R,L]
RewriteRule ^(.*)%{HTTP_HOST}$ $1%{HTTP_HOST}/prod/index.php [R,L]
RewriteRule ^(.*)%{HTTP_HOST}/$ $1%{HTTP_HOST}/prod/index.php [R,L]
</IfModule>
#END Aldersoft Permalink Fix

This essentially says:

  • Is the Apache rewrite module loaded?
  • If so, then start the Rewrite Engine
  • Set the URL pointer to the end of the start of the
  • If there was no data passed following the home directory,
    Then rebuild the URL to point to https://MySite.com/prod/index.php
    EG. https://MySite.com/prod will have %{HTTP_HOST} of “MySite.com”
    This will be rebuilt as https://MySite.com/prod/index.php
    Lastly [R,L], says redirect (302) to the new URL, leave .htaccess, and start again.
  • If the URL ends immediately after the %{HTTP_HOST} EG. https://MySite.com or https://MySite.com/prod
    Then rebuild the URL to point to https://mysite.com/prod/index.php
    Lastly [R,L], says redirect (302) to the new URL, leave .htaccess, and start again.
  • If the URL ends with a / immediately after the %{HTTP_HOST}/ EG. https://mysite.com/prod/
    Then rebuild the URL to point to https://MySite.com/prod/index.php
    Lastly [R,L], says redirect (302) to the new URL, leave .htaccess, and start again.

Note. Thank you Jacob Buller of EcoVillages.CA for your help in debugging this.