I currently have a .htaccess file with the current lines to force HTTPS:

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

My main domain is example.com I have a registered an alias in my cPanel: example.net

I would like to implement a rule so that if a user visits example.com, they will be redirected to https://example.net.

I've tried a few things and kept getting redirection errors. Can someone help me out, not sure what I should be doing here?

  • What about www and other subdomains?
    – DocRoot
    Oct 4 '18 at 18:10

Try the following instead:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.net$
RewriteRule .* https://example.net%{REQUEST_URI} [R=301,L]

If HTTPS is off or the requested host is not example.net then redirect to HTTPS and example.net.

  • 1
    Should be RewriteRule .* https://domain.tech%{REQUEST_URI} [R=301,L] Oct 4 '18 at 18:21
  • @user1692517 No, have a look at httpd.apache.org/docs/2.4/rewrite/intro.html#regex for a clear example on % and $ in mod_rewrite, they serve different purposes. Oct 4 '18 at 20:25
  • 1
    @PatrickMevzek This isn't a backreference (where you have %n and $n), it's a reference to a server variable of the form %{SERVER-VARIABLE}. I'd made a typo in my answer - now updated.
    – DocRoot
    Oct 4 '18 at 21:56

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.