Martin AJ
Martin AJ

Reputation: 6697

How to redirect ip to domain name?

I use Apache web-server and I want to redirect all urls from ip to domain. Here is the content of my website .htaccess file:

RewriteEngine on

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

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

According to some research, I guess I need something like this: (not sure)

RewriteBase /
RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^([\s\S]*)$ https://lamtakam.com/$1 [L,R=301]

But I don't know how (in which part) should I add those threelines to the .htaccess file. Any clue?

Upvotes: 1

Views: 721

Answers (1)

anubhava
anubhava

Reputation: 785128

You should insert IP redirect rule before or after https/www redirect rule:

Options -Indexes
RewriteEngine on

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

RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^ https://lamtakam.com%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

If you want to make your IP matching condition more generic then use:

RewriteCond %{HTTP_HOST} ^\d+\.\d+\.

to be able to match any IP address.

Upvotes: 2

Related Questions