Access Website by domain name only (not IP address)
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, , , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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.
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]
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.
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.
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:
Comments
Post a Comment