What is the .htaccess file?
.htaccess is a configuration file for use on web servers running the Apache Web Server software. .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. It includes basic redirect functionality. for instance, if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hotlink prevention.
How to use the .htaccess file?
.htaccess files provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
When to use the .htaccess file?
The .htaccess file should be used when the main server configuration file cannot be accessed or modified. It is always best to use the server configuration files over .htaccess files.
The following htaccess will able to help you to achieve a simple task such as redirection and web server optimization;
- How to create custom error pages
ErrorDocument 404 errors/404.html
- Control access at files & directory level
If you don’t want to give access to the user for accessing a file directly, over the web than drop a .htaccess file in that folder when this file is placed with content something like this.
# no one gets in here! deny from all
– If you want to deny all for direct access and limit access to particular ip range, then add following in your .htaccess.
order deny,allow deny from all allow from 192.167.0.0/24
– If you want to ban one ip address and allow to access all than add following in your .htaccess.
order allow,deny deny from 83.212.24.219 allow from all
- 301 Redirect
- If you want to redirect from an old document to new:
Redirect 301 /old/file.html http://yourdomain.com/new/file.html
- Use following for redirecting Entire Directory.
RedirectMatch 301 /blog(.*) http://yourdomain.com/$1
- Rename your .htaccess file
AccessFileName htacc.ess
- Change the default page index.html, index.php or index.htm to something else.
DirectoryIndex project.html
- Change Charset and Language headers
AddDefaultCharset UTF-8 DefaultLanguage en-US
- Set Timezone of the Server (GMT)
SetEnv TZ Australia/Melbourne
- Force “File Save As” Prompt
AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4
- Compress file
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE application/x-httpd-fastphp AddOutputFilterByType DEFLATE image/svg+xml SetOutputFilter DEFLATE
- Catch file
Header set Cache-Control "max-age=2592000"
- Disable caching for certain file type
Header unset Cache-Control
- Disallow Script Execution
Options -ExecCGI AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
- Avoid the server from displaying directory index, or the opposite.
# disable directory browsing Options All -Indexes # enable directory browsing Options All +Indexes
- Prevent user to access your .htaccess file. Also, you can block multiple file type as well.
order allow,deny deny from all # prevent viewing of a specific fileorder allow,deny deny from all # multiple file typesOrder Allow,Deny Deny from all
I hope this post helps you and if you have any query then comment below. You can also contact our experienced developer. Our experienced developer at Lathiya Solutions will be happy to help you.