1

I have a website and have recently installed an SSL certificate. I want all links to the site to always redirect to the secure site without www, as I have made the SSL applicable to non-www.

Furthermore, I have WHMCS in the backend and want to redirect the example.com/whmcs page to example.com/client-portal as I have a third party plugin that links with whmcs.

What should I insert in my .htaccess file and in what folders should I insert the file? Can I specify everything from root and if this is the case, how do I force WHMCS to be happy with this?

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] 
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

Edit

The good news is that I have solved the first part of the problem. All URLs now redirect to the non-www https website. The code that solved this issue was supplied by @John Conde - thanks John!

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

My next goal is to have example.com/whmcs redirect to example.com/client-portal.

...but without redirecting example.com/whmcs/admin to example.com/client-portal/admin. In other words, /whmcs/admin should not redirect.

I have tried the following solution but without success. I'll appreciate it if you guys can help.

RewriteCond !^example\.com\/whmcs\/admin
RewriteCond ^example\.com\/whmcs$
RewriteRule https://example.com/client-portal [R=301,L]

My intention was that this causes /whmcs to redirect but not for /whmcs/admin to redirect.

4
  • Show us what you've tried
    – John Conde
    Commented Oct 7, 2014 at 11:40
  • 1. Do servers remember permanent redirects or do only browsers remember them? 2. Why does WHMCS not redirect to https if there is a redirect set up in CPanel? I did not see any htaccess file in my WHMCS root directory. 3. Why does some people put NC agent rewrite conditions? This does not seem necessary. 4. How do I know what will be in variables, e.g. S1? 5. Is %1 also some sort of variable? 6. Does the request URI variable hold the site URL with or without (or both) www?
    – TJVR
    Commented Oct 7, 2014 at 12:09
  • I have tried a couple of things. My latest try was to use : RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
    – TJVR
    Commented Oct 7, 2014 at 12:10
  • The problem I had here (I think it was this one) was that when I tried to access WHMCS it redirected to my home page.
    – TJVR
    Commented Oct 7, 2014 at 12:12

3 Answers 3

1

Not sure if this would encompass your goal completely, but this would turn http www mode into https non-www mode:

RewriteEngine On

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

Then to redirect the whmcs to a new url, you can probably use a simple 301: Redirect 301 /whmcs /client-portal

These would both go into the root of your app, or optionally at the place where an A record is picked up.

3
  • Thanks for this. I have tried to employ it but it lacks one final thing. I only want /whmcs to be redirected to /client-portal and not /whmcs/admin. I have tried this code but it redirects /whmcs/admin to /client-portal/admin which does not exist.
    – TJVR
    Commented Oct 7, 2014 at 16:43
  • Ah i see. Does the 301 work if you point /whmcs/admin to /client-portal? Also is there a htaccess inside the /whmcs or /whmcs/admin folder? Maybe just placing it differently/deeper will help to clean off that /admin
    – dhaupin
    Commented Oct 7, 2014 at 16:49
  • Yes, the 301 works but it ends up getting a url that is not available on the site. I don't see the logic in placing it deeper since if it goes into the /whmcs directory and finds the .htaccess file there it will redirect to /client-portal. This will also be the case for /whmcs/admin. Regarding your other question, no, I have removed all .htaccess files in subfolders.
    – TJVR
    Commented Oct 7, 2014 at 16:53
1

You could also take the approach of multiple rewrite rules. Replace example.com with your actual domain name.

RewriteEngine On 

RewriteCond %{HTTPS} off
# HTTPS is off!
# Redirect to the secure canonical URL
RewriteRule (.*) https://example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
# Host name is not correct!
# (maybe it has a www?)
# Redirect to the secure canonical URL
RewriteRule (.*) https://example.com/$1 [L,R=301]

As for the succeedonline.co.za/whmcs redirect, you can use a single simple rewrite rule for that:

RewriteRule ^whmcs$ https://example.com/client-portal [L,R=301]

The ^ means "starts with" and the $ means "ends with", so that redirect will only get triggered when the URL is exactly succeedonline.co.za/whmcs and not when it is succeedonline.co.za/whmcs/admin.

2
  • I wonder whether the 2nd RewriteRule is necessary? Since the SSL cert is only valid for the bare domain, wouldn't the user be hit by an invalid cert warning first if they tried to access https://www.example.com?
    – MrWhite
    Commented Oct 7, 2014 at 18:51
  • I buy wildcard certs that cover multiple domain names, so I use such a rule. Commented Oct 7, 2014 at 18:52
1

I realized that it does not yet redirect the non-www site under the HTTPS protocol. It does however work fine under the HTTP protocol. Here is an extract of my complete .htaccess file:

RewriteOptions inherit

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] 
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] 
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

#RewriteCond !^example\.com\/clients\/bestuur
#RewriteCond ^example\.com\/clients$
#RewriteRule https://example.com/client-portal [R=301,L]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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