How to redirect https://ip to https://domain

Posted July 27, 2019 16.1k views
ApacheServer OptimizationUbuntu 18.04

The following lines are in the .htaccess I wrote.

<IfModule mod_rewrite.c>  
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

It redirects when I type this in the browser.

But when I write like this, it doesn’t redirect.

I bought SSL certificate for two years and there was no problem with the certificate.

Can anyone tell me what its rewrite condition is ?

edited by MattIPv4
1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers


Do you have a VirtualHost entry matching the IP address with https and not http? If there is an entry for the IP address with https this will be the reason.

Create a VirtualHost entry matching as ServerName, but not using https, but http instead. Then redirect this to your domain name.

Let me know how it goes.


  • HTTP Virtual Host with ServerName:

    HTTPS Virtual Host on ServerName:

    Are you asking for an IP address on HTTPS Virtual Host ?

    There is no IP address given to my virtual hosts.

    HTTPS will not access any HTTP virtual hosts.

    Do you mean that the problem is in the ServerName ?

    Full HTTPS Virtual Host Write it down pls, so what would that be like.

    • Thanks for confirming this.

      I just wanted to ask if everything is fine with the virtual host configuration.

      Also I have a question, when you access https:/ you should see a certificate warning page and then you should be redirected to the site itself. You might have already acknowledged the certificate warning, but this is something that will happen for sure.


      edited by MattIPv4
      • Yes you are right.

        There it says “Your connection is not private - NET::ERRCERTCOMMONNAMEINVALID”

        Do you think that HTTPS is never redirected ?

        Or it would have redirected but it is not being redirected because of the certificate ?

        Is it stuck on the certificate ?

        Or the virtual host is fine, Rather, it should be redirected from .htaccess

        • This is the normal behavior of a web server, because there is no SSL Certificate installed for the IP address itself and the browser will always give you this warning message regarding the certificate. It’s just how the web server works.

          Let me know if you have any questions.


          edited by MattIPv4


I am having this same issue. I have tried to create a new VirtualHost entry as my ServerName but the https://ip is not redirecting to https://domain.

Currently the http://ip is redirecting to the https://domain.

When I tried to create a redirect on the VirtualHost on port 443 all my entry points ( https://ip and https://domain ) stop working, giving a ‘not allowed to acess this resource error’.

ServerAdmin webmaster@localhost
DocumentRoot /dev/null
ServerName http://ip

I have the same problem, any solution ?