STS Host and Tech Knowledge base
Search
Close this search box.

Htaccess Tutorial

What is .htaccess ?

It is a configuration file placed on directory level. And contain certain instructions. These instructions tell you how incoming traffic to your website will be handled.
For example, accessing a website, this can be for URL redirect. URL shortening or an access control. Moreover, There are a number of things that can be recorded in the .htaccess file.

In addition,You can create a it using any good text editor such as TextPad, UltraEdit, Microsoft WordPad and similar (you cannot use Microsoft NotePad).

Examples for configurations you can add to the file:

Here are some codes to use for configuration . Add each code to the .htaccess .

  • To remove PHP HTML Extensions From THE URL add the following code inside your .htaccess file:

For PHP

				
					RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
				
			

For HTML

				
					RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
				
			

To remove Libwww-perl Access Test add this code inside the file:

				
					RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
				
			

In order to improve the security of your site against some types of XSS (cross-site scripting) attacks, it is recommended that you add the following header to your site:

				
					<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
</IfModule>
				
			

To add browser caching to your website, you have to set the date for when the cache expires:

				
					<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
				
			

To display custom Error Message on common error number 404, the simplest way to set the 404 error page is by setting directly the 404 error message in the .htaccess file itself:

				
					ErrorDocument 404 "SORRY Page not found"


				
			

Or if you have custom Error 404 Like “404.html” Page you can simply add the following code to the .htaccess

				
					ErrorDocument 404 /404.html

				
			

Share Article

Related Articles