
.htaccess stands for “Hypertext Access”, it’s a configuration file that is used by Apache-based web servers.
- Log in to the hosting CPanel.
- Go to File Manager
- Go to the public_html directory.
- Look for the .htaccess file, right-click on it, and choose edit.
How to redirect www URLs to non-www?
To redirect all requests to non-www, add the following lines at the beginning of your website’s .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Replace yourdomain.com with your actual domain name.
How to redirect non-www URLs to www?
To redirect your website from non-www to www, add the following lines in your website’s .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Replace yourdomain.com with your actual domain name.
How to deny all and allow only allowed IP?
deny from all
allow from 111.222.333.444
allow from 555.666.777.888
Replace the above fake IP with your public IP with your actual domain name.
URL Masking with .htaccess
The following method will allow you to mask a certain part of your website’s address.
For example, when someone opens up:
http://yourdomain.com/something
the actual content that will be displayed can be the one under:
http://yourdomain.com/something/else
although the URL in the visitor’s address bar will still remain http://yourdomain.com/something.
To achieve this, add rules similar to the ones below into your website’s .htaccess file:
RewriteEngine On
RewriteRule ^something/?$ /something/else/
Make sure to substitute something and else which the actual directories that you are using on your website.