Redirect IP to Domain Name in Linux
How to redirect IP to FQDN with HTTPS in Apache Webserver using mod_rewrite (Ubuntu 18,20,22)?
Overview: This article guides you through redirecting an IP address to an FQDN with HTTPS in Apache on Ubuntu 18, 20, and 22 using mod_rewrite. It includes enabling required modules, configuring virtual hosts for HTTP and HTTPS, modifying Apache settings to allow overrides, and setting up .htaccess rules to enforce redirection from http://192.168.1.1 to https://server.gateway.com/ezlogin/.
The objective of the article is to rewrite the URL from http://192.168.1.1 to https://server.gateway.com/ezlogin using .htaccess
Step1. Enable the rewrite and alias module in Ubuntu.
root@ubuntu ~]# a2enmod rewrite alias
root@ubuntu ~]# systemctl restart apache2
Step 2. Create a virtual host for the domain name with ports 80 and 443.
root@ubuntu ~]# vim /etc/apache2/sites-available/server.gateway.com.conf
root@ubuntu ~]# vim /etc/apache2/sites-available/server.gateway.com-ssl.conf
root@ubuntu ~]# vim /etc/apache2/sites-available/000-default.conf
root@ubuntu ~]# vim /etc/apache2/sites-available/default-ssl.conf
root@ubuntu ~]# vim /var/www/html/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^server.gateway.com$
RewriteRule ^(.*)$ https://server.gateway.com/$1 [R=301,L]
RedirectMatch ^/$ https://server.gateway.com/ezlogin/
Related Articles: