The .htaccess is the most sensitive Apache server configuration file. You can perform several sensitive tasks by writing a small code inside the htaccess file. You can use the .htaccess file to configure a server for a specific directory.
The .htaccess file is present by default inside the root directory, or you can also create one. The htaccess file is very useful in many cases such as authorization, error handling, user permissions, redirects, etc.
Today, in this tutorial, we're gonna walk you through all the tasks you can perform with the .htacess file.
Tasks you can do with the .htaccess file
DirectoryIndex index.html
It is also possible to add multiple default homepages at once. In this case, the .htaccess file will send a command to the server to check the open website with first landing page, and if the first one isn't present then it'll go for the second, and so on.
DirectoryIndex index.html home.html custom.html
- Deny Access To Files: The following command will through a 403 forbidden error when a visitor will hit any .inc file.
- Block any desired IP: The below command will block a specific IP address to browse your website. Change the dummy IP address with your custom one.
- Block multiple IPs: Block more than one IP address at once by following the below command.
- Block all IPs and allow only your: With the below command, you can block everyone from visiting your website except yourself. You just have to replace the dummy IP address with your own IP address which you can find from the IP Lookup website.
Or you can simply use the below code
Redirect 301 / https://newdomain.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Example 1: redirect errors to html files
ErrorDocument 404 /404.html
Example 2: redirect errors to PHP file
ErrorDocument 404 /error.php?q=404
AuthUserFile /path/.htpasswd
AuthType Basic
require valid-user
Note: You'll have to have a .htpassword file where the username & password will have to be present. However, you can create it on your own by using any editor such as notepad, notepad++, etc.
No comments:
Post a Comment