2

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
2

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] – user1692517 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. – Patrick Mevzek Oct 4 '18 at 20:25
  • @user1692517 Thanks, yes. Answer updated. – DocRoot Oct 4 '18 at 21:48
  • 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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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