Macbernie Macbernie - 9 days ago 8
Linux Question

Forbid access to website through IP server

I am hosting my website on my Debian server. My website is accessible by is domain name https://www.domainname.fr

But nothing prevents users to access the website by the IP of the server /myhostname, so http://serveripadress/domainname/

I put a

Options All -Indexes


in the .htaccess to Forbidden the access to the website repositories, but I don't want the users navigates on the site by the http://serveripadress/domainname/ url...

I need to automatically redirect people from http://serveripadress/domainname/ to the real domain name https://www.domainname.fr

How can I do that ? Is there other kind of access protection to put on the .htaccess ?

Here is my current .htaccess content:

#FallbackResource /index.php

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# D├ęsactiver l'exploration des r├ępertoires web
Options All -Indexes


Thanks for help

Answer

You can use this rule in your site root .htaccess:

Options All -Indexes
RewriteEngine On
RewriteBase /

# if hostname in current request is not www.domainname.fr
RewriteCond %{HTTP_HOST} !^(?:www\.)?domainname\.fr$ [NC]
# then redirect
RewriteRule ^ http://www.domainname.fr%{REQUEST_URI} [L,NE,R=301]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]