1

I created a 301 redirect in my cPanel to force ssl. It is not working. The last two lines were created with the cpanel for the redirect.

RewriteOptions inherit
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
</Files>

# END Wordfence WAF

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example\.com\/$ "https\:\/\/example\.com\/" [R=301,L]

Why isn't this working?

1

I created a 301 redirect in my cPanel to force ssl.

Unfortunately, cPanel's "redirect feature" is a bit notorious for making a mess. And since an incorrect directive can bring down a site, it's difficult to recommend. If you are going to try and use it then you really need to read the cPanel documentation to make sure of the expected inputs and some caveats. (The on-page instructions with the tool itself are a bit lacking IMO.)

However, the redirect feature provided by cPanel is a bit limited. You can only really do simple redirects. A "force SSL" type redirect is not possible using this tool.

Why isn't this working?

Well, to be frank, it's completely wrong. (The cPanel docs linked to above would have helped here.)

  1. "The last two lines" - The first fundamental error here is that these directives are placed at the end of the file. Because of the WordPress front-controller at the top of the file, these directives will never be processed (even if they were correct). (This is covered in the cPanel docs.)

  2. RewriteRule ^example\.com\/$ - The RewriteRule directive matches against the URL-path only, not the hostname. So, this will never match. (Again, the cPanel docs covers the expected input.)

  3. "https\:\/\/example\.com\/" - To implement this properly you would need some kind of "wildcard" redirect, redirecting all URLs like for like. This directive would redirect to the home page only. (It's also an example of cPanel's "mess" - there is never a need to backslash escape colons, slashes and dots in the RewriteRule substitution. The surrounding quotes are also unnecessary here, but that's forgivable given the automated nature of this generated output.)

  4. In order to redirect from HTTP to HTTPS, you need to first check that the current request is not already for HTTPS, otherwise you'll get a redirect loop.

  5. RewriteCond %{HTTP_HOST} ^.*$ - This is just nonsense - it doesn't do anything. This directive will always evaluate to true on any system.

RewriteOptions inherit

This directive is also suspicious. Do you need this? If you do, then this can influence where this "force SSL" redirect should go (before, after, or in a different config file altogether).

The appropriate redirect code will need to go near the start of your .htaccess file, before the existing WordPress directives (ie. before the front controller). In its simplest form, this would be something like:

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://example.com/$1 [R=301,L]

This assumes that example.com is the canonical hostname (as in your code snippet). To incorporate a www to non-www redirect as well, then change this to:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://example.com/$1 [R=301,L]
| improve this answer | |
0

What version of preferred HTTPS URL would you like to have?

Let's say if you would like to have: https://www.example.com then do, following and it should work.

Change the setting accordingly in Wordpress (Site / WordPress Address) and then change your .HTACCESS as below:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=301,L] 
RewriteCond %{http_host} ^example.com/blog/ [NC]
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=301,L] 

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

Note: Here WordPress installation is on /blog/ so you can change it as per your need.

| improve this answer | |
  • 1
    The HTTP_HOST server variable contains the hostname only (not the URL-path) so your RewriteCond directive will never match and https://example.com would never be redirected. But why reference a /blog subdirectory anyway, there is no mention of this in the question? Your canonical redirects will also expose the subdirectory and break if the /blog subdirectory was accessed directly. – MrWhite May 7 '17 at 10:37
  • As explained the question itself is not very clear. Above example code is tested. Based on clarification code can be easily changed. This is just a reference how it can be fixed. – TopQnA May 7 '17 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.