You can change a default index file of directory by using above snippet in your htaccess file. If a user request /foo/, Apache will serve up /foo/index.html, or whatever file you specify.Code language: HTML, XML (xml)DirectoryIndex index.html index.php index.htm
You may want to redirect your users to an error page is any of the http errors like 404 occurs. You can use above snippet in htaccess file to map 404 error to error page errors/404.html. Also you may want to write a common page for all the http errors as follows:Code language: HTML, XML (xml)ErrorDocument 404 errors/404.html
Code language: HTML, XML (xml)ErrorDocument 404 /psych/cgi-bin/error/error?404
which would deny ALL direct access to ANY files in that folder. You can be more specific with your conditions, for instance limiting access to a particular IP range, here’s a handy top-level rule for a local test server.Code language: HTML, XML (xml)# no one gets in here! deny from all
Generally these sorts of requests would bounce off your firewall anyway, but on a live server they become useful for filtering out undesirable IP blocks, known risks, lots of things. Sometimes, you will only want to ban one IP, perhaps some persistent robot that doesn’t play by the rules.Code language: HTML, XML (xml)# no nasty crackers in here! order deny,allow deny from all allow from 192.168.0.0/24 # this would do the same thing.. #allow from 192.168.0
Code language: HTML, XML (xml)# someone else giving the ruskies a bad name.. order allow,deny deny from 83.222.23.219 allow from all
Code language: HTML, XML (xml)SetEnv SITE_WEBMASTER "Jack Sprat" SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com UnSetEnv REMOTE_ADDR
Use following for redirecting Entire Directory.Code language: HTML, XML (xml)Redirect 301 /old/file.html http://yourdomain.com/new/file.html
Code language: HTML, XML (xml)RedirectMatch 301 /blog(.*) http://yourdomain.com/$1
# year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
Code language: HTML, XML (xml)
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
Code language: HTML, XML (xml)
Above code works only if mod_gzip module is enabled in your webserver. You may want to add following snippet if your webserver provides mod_deflate support. <Location>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>
Code language: HTML, XML (xml)
If your webserver does not support mod_deflate then you may want to use following snippet. <FilesMatch "\.(txt|html|htm|php)">
php_value output_handler ob_gzhandler
</FilesMatch>
Code language: HTML, XML (xml)
Read this articles for more detail: Compressing PHP, CSS, JavaScript(JS). Code language: HTML, XML (xml)RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Rewriting product.php?id=12 to product/ipod-nano/12.htmlCode language: HTML, XML (xml)RewriteEngine on RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
Redirecting non www URL to www URLCode language: HTML, XML (xml)RewriteEngine on RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyzCode language: HTML, XML (xml)RewriteEngine On RewriteCond %{HTTP_HOST} ^viralpatel\.net$ RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]
Redirecting the domain to a new subfolder of inside public_htmlCode language: HTML, XML (xml)RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
Code language: HTML, XML (xml)RewriteEngine On RewriteCond %{HTTP_HOST} ^test\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.test\.com$ RewriteCond %{REQUEST_URI} !^/new/ RewriteRule (.*) /new/$1
orCode language: HTML, XML (xml)Options -Indexes
Read this article on more details on Denying/Allowing directory listing.Code language: HTML, XML (xml)IndexIgnore *
Code language: HTML, XML (xml)AddType application/x-endnote-connection enz AddType application/x-endnote-filter enf AddType application/x-spss-savefile sav
Code language: HTML, XML (xml)RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC] RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC] RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [F,NS,L]
In the above .htaccess file, uploading capability is increased by the four parameter first one is maximum file size for uploading, second one is maximum size of the post data , third one is maximum time in seconds a script is allowed to run before it is terminated by the parser and last one is maximum time in seconds a script is allowed to parse input data such as like file uploads, POST and GET data.Code language: HTML, XML (xml)php_value upload_max_filesize 20M php_value post_max_size 20M php_value max_execution_time 200 php_value max_input_time 200
Code language: HTML, XML (xml)Options -ExecCGI AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Code language: HTML, XML (xml)AddDefaultCharset UTF-8 DefaultLanguage en-US
Code language: HTML, XML (xml)SetEnv TZ America/Indianapolis
Code language: HTML, XML (xml)AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4
<Files quiz.html>
order deny,allow
deny from all
AuthType Basic
AuthName "Characterology Student Authcate"
AuthLDAP on
AuthLDAPServer ldap://directory.characterology.com/
AuthLDAPBase "ou=Student, o=Characterology University, c=au"
require valid-user
satisfy any
</Files>
Code language: HTML, XML (xml)
Set Cookie based on Request. This code sends the Set-Cookie header to create a cookie on the client with the value of a matching item in 2nd parentheses.Code language: HTML, XML (xml)Header set Set-Cookie "language=%{lang}e; path=/;" env=lang
Code language: HTML, XML (xml)RewriteEngine On RewriteBase / RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/]
Code language: HTML, XML (xml)Header set P3P "policyref=\"http://www.askapache.com/w3c/p3p.xml\"" Header set X-Pingback "http://www.askapache.com/xmlrpc.php" Header set Content-Language "en-US" Header set Vary "Accept-Encoding"
Feel free to bookmark this article.Code language: HTML, XML (xml)SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT Deny from env=HTTP_SAFE_BADBOT
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Hey viral.. Its nice post. Regarding .htaccess, I am facing one issue from last few days. .htaccess files is getting deleted automatically every 2/3 days on server. I am very much sure its some server process initiated by hosting company. I contacted hosting provided but reply was not very helpful. I found that, some hosting provider do this but not clear documentation is there. If you have any idea, let me know.
Thanks
Hi Nilesh,
I am not sure if I ever had this type of problem.!! This is quite strange and the webserver is seem to be culprit as you have mentioned in your comment.
I will let you know if I find something in this direction. :)
Hello,really nice.
hi
just ti day in tips 13 that you need to put max php memory higher than max upload file size too if memory limit is 16M you can't upload a 20M file.
It's so comprehensive! Thanks
Many many thanks for this useful tutorial.
greate very useful tips
Hi Viral,
I am unable to find my website on Google after adding www on it.
can u plz assist.
Thank you for the great information. I have so many redirects in htaccess. I wish I could cut down on them.
Thank you very much, greate information and really it is very useful tips