Mobile app version of vmapp.org
Login or Join
Ravi8258870

: Access Website by domain name only (not IP address) Let's say I have a website example.com and it points to 12.23.42.31. Now I want that nobody can access my website by the IP address (12.23.42.31).

@Ravi8258870

Posted in: #Htaccess

Let's say I have a website example.com and it points to 12.23.42.31. Now I want that nobody can access my website by the IP address (12.23.42.31). What is the procedure to achieve this?

LAMP server with WordPress. Not shared hosting.

10.05% popularity Vote Up Vote Down


Login to follow query

More posts by @Ravi8258870

5 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

If you want to block all users that trying to access your website with other way than your domain name, you can do this :

<VirtualHost example.com:80>
ServerName example.com
ServerAdmin webmaster@example.com
UseCanonicalName Off
<If "tolower(%{SERVER_NAME}) != 'example.com'">
AllowOverride None
Require all denied
</If>
</VirtualHost>


All access trying in other way than example.com will result on :


403 Forbidden

You don't have permission to access / on this server.




OTHER WAY, DIFFERENT RESULT :

If you want to make a rewrite rule, so all access trying in other way than example.com will result on a redirect to example.com, you can do a simple redirect rule like this :

RewriteCond %{HTTP_HOST} !^example.com
RewriteRule ^(.*)$ example.com/ [R=301,L]


For www :

RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule ^(.*)$ www.example.com/ [R=301,L]

10% popularity Vote Up Vote Down


 

@Shakeerah822

Because you mentioned this is for a LAMP server and you have SSH access, you can do this quite simply with a 000-default VirtualHost like this, (changing example.com to your domain):

<VirtualHost *:80>

Redirect permanent / www.example.com/
</VirtualHost>


Rewrite methods are unnecessary, and redirecting provides better security against host header attacks. By skipping the usual .htaccess or PHP methods and doing a redirect you get the best possible performance, though this probably isn't a concern if most requests are for your domain and not the IP.

For completeness, here's how you do it with Nginx:

server {

listen 80 default_server;
return 444;

}


One slight difference, Nginx's 444 response means 'no response' and it immediately drops the connection. In the unlikely event that you would want to use a redirect instead you can change the return 444 directive to return 301 www.example.com/.
It is really unlikely that legitimate traffic from your users is requesting your site by it's IP address, so don't waste much time trying to accommodate it. Drop or redirect the traffic instead.

10% popularity Vote Up Vote Down


 

@Si4351233

Here are some redirect options you can use in .htaccess. These cover various cases of raw IP, the IP somehow mucked with www, util host subdomain (for addon domain), target www mode, target HTTPS mode, etc. This will also preserve any URi's instead of just dumping to a static homepage or whatever. Swap/edit the last 2 lines for www and HTTPS mode:

RewriteCond %{HTTP_HOST} ^(www.)?123.123.123.123 [OR]
RewriteCond %{HTTP_HOST} ^(www.)?example.hostname.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ "https://www.example.com/" [R=301,L]

10% popularity Vote Up Vote Down


 

@Goswami781

If you do not want the site to respond to the IP Address I would implement a virtual host in Apache. Set the virtual root to a different directory of the Apache server.

Now all traffic defaults to a different location of your manned site.

Otherwise as already noted I would use a redirect. Personally I would use the htaccess style. After tested in the .htaccess file I would move the instructions to my virtual host conf file.
Noting that I always have full access to my private servers.

EDIT:
Did you change your httpd.conf aka apache2.conf? If not, no worries your default route should be /var/www/html If you did change this, I would recommend putting it back.

Here is an example Apache Conf File for one of my Developement Test sites Running Apache 2.2 on CentOS 6.5. If you hit the hosting server IP or default name then you will find the Apache test page or a simple white page with no content.

NameVirtualHost *

<VirtualHost *>
ServerName search??.info
ServerAlias *.search??.info
DocumentRoot /data/steven/cms5/test
<Directory /data/steven/cms5/test>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>


Following is an example of configuration file on my (Mint 17) Ubuntu based workstation. Again, if you reference the IP Address or the default server name you will will encounter a default page.

<VirtualHost *:80>
ServerAdmin k7faq.az@gmail.com
ServerName cms5.dev
ServerAlias *.cms5.dev
DocumentRoot /var/www/html/cms5/public
<Directory /var/www/html/cms5/public>
Options All
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

<IfModule mod_rewrite.c>

<Directory "/var/www/html/cms5/">
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/ [L]
</Directory>

<Directory "/var/www/html/cms5/public">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/ [QSA,L]
</Directory>

</IfModule>


Also, you can see here that I moved the .htaccess directives to this conf file.

10% popularity Vote Up Vote Down


 

@Annie201

Easy. Just make a redirect from the IP address to the domain name.

Method 1

A nice way to do it if your server supports it is to create a PHP file containing the following:

<?php
header("HTTP/1.1 301 Redirect",true);
header("Location: example.com ,true)
?>
<html><head><title>Redirect</title><body>
<a href="http://example.com">Click here to continue</a>
</body></html>


then save it as index.php in the document root folder of your ip address (probably in the htdocs folder).

Method 2

If your server is apache, has the rewrite module installed and allows configuration changes via .htaccess files, then create that file and save it in the document root of your ip address with the following contents:

RewriteEngine On
RewriteRule ^(.*)$ example.com [R=301,L]


Nice thing with method 2 is that it redirects ANY url starting with the IP address to the homepage at example.com.

Just make sure you put all your website files in the document root folder of example.com.

and of course, replace example.com in my answer with your domain name.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme