3

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.

  • What web server? As well, do you have shell access? As well, if you are on shared hosting, there is nothing you can do. – closetnoc Jun 7 '16 at 4:26
  • It is simple lamp server with wordpress, and i do have shell access of the server and it is not shared hosting. – amit singh Jun 7 '16 at 4:32
2

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 / http://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 http://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.

| improve this answer | |
  • "In the unlikely event that you would want to use a redirect instead" - If it's "unlikely" then why show a redirect as the preferred solution on Apache? – MrWhite Nov 6 '16 at 20:46
  • @w3dk The answer is in the paragraph I wrote - Nginx has a 444 response that is preferred. The paragraph is about Nginx. Apache has nothing to do with my recommendation, it's covered in the preceding paragraphs. Feel free to edit my answer to make it clearer if you like. – Tom Brossman Nov 7 '16 at 10:04
5

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 ^(.*)$ http://example.com/$1 [R=301,L]

For www :

RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
| improve this answer | |
2

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/$1 [L]
</Directory>

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

</IfModule>

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

| improve this answer | |
1

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/$1" [R=301,L]
| improve this answer | |
0

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: http://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 ^(.*)$ http://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.

| improve this answer | |
  • Dang you are fast!! – closetnoc Jun 7 '16 at 4:36
  • 1
    BTW- ^(.*)$ redirects everything. – closetnoc Jun 7 '16 at 4:38
  • 1
    "redirect from the IP address to the domain name" - yes. However, both your code examples appear to redirect all traffic? (It's not a shared host, so the IP address and domain name will resolve to the same place on the filesystem.) – MrWhite Jun 7 '16 at 9:36
  • 1
    If the IP address and the domainname serve the same content, this will loop into a fail. You should implement a test to see if there is an IP (or alternatively, if servername!=example.com). – Martijn Jun 7 '16 at 14:45
  • Yes I forgot to mention to insert that rule into the IP address virtual host space. – Mike -- No longer here Jun 8 '16 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.