apache tutorial - Features of .htaccess - apache - apache web server - apache server - apache2



.htaccess features 1 : htaccess redirect - Redirecting an Old Link to a New One Via the 301 Page Error using .htaccess apache file

Problem :
  • One of the biggest problems that many websites encounter when moving content is that their search engine ranking drops dramatically right after the move.
  • The reason is, old content is not properly relocated to the new location, and the search engine itself is never informed that a move took place.
  • Instead, the search engine encounters “ page not found “ 404 Error, which is essentially like telling the search engine that it has arrived at a dead end.
  • That dead end will force the search engine to remove the link from its search results entirely, and the website’s reputation will be lost as if it is starting from nothing.

Solution - 301 redirect htaccess for the “page not found” problem:

  • To prevent this, search engine optimization professionals recommends an intelligent option of using a htaccess 301 redirect as part of the .htaccess file. This error is known as a “permanent redirect,”
  • htaccess redirect 301 will redirect the search engine towards the new location of a file or a specific block of content. In addition, redirect htaccess will inform the search engine that the content has been moved on a permanent basis, and will not be moving back.
  • This will prompt and makes the search engine robot to update the link in its present search results, rather than remove it entirely and keeps the website to maintain its high ranking.


.htaccess features 2 : Disabling Directory Browsing

To prevent directory browsing, add this:
Options All -Indexes
click below button to copy the code. By Apache tutorial team
However, if you want to enable directory browsing, change it to the following:
Options All +Indexes
click below button to copy the code. By Apache tutorial team

.htaccess features 3: htaccess rewrite - Protect Your Site against Hotlinking

If you don’t want your images hotlinked, add htaccess rewrite to your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?wikitechy.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
click below button to copy the code. By Apache tutorial team
Just replace wikitechy.com with your website and you can put this code in .htaccess file in your apache web server,

.htaccess features 4 : Caching Files

To incrase the website load time, you can use the option of file caching. The old users will have the cache files in their machine and it will be loaded which makes the website faster,

Below is the procedure to cache files using .htaccess file

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
click below button to copy the code. By Apache tutorial team
You can add more file types (or remove some of them) to the sequence of files listed in this example – do what suits you. You can also use max-age to specify the amount of time in seconds that your files will live in the cache.

.htaccess features 5 : Disabling Caching for Particular File Types

  • In case, If you don’t want to cache particular file types, it is easier not to include them in the cache sequence.
  • Sometimes files might get cached even if you don’t explicitly list them there and in this case you may want to disable caching only for them.
  • Most often you will want to disable caching for dynamic files, such as scripts.
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
click below button to copy the code. By Apache tutorial team

Just pipe the files you want caching disabled and put this code .htaccess file

.htaccess features 6 : htaccess http to https - Redirecting to a Secure https Connection

If you are using https and in case, you want to redirect http to https htaccess . use the below htaccess https code,
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
click below button to copy the code. By Apache tutorial team

.htaccess features 7 : Redirect to Maintenance Page + allowing particular IP

  • Redirecting visitors to a maintenance page or other temporary page is an essential tool to have in your tool belt. Using HTAccess, redirecting visitors to a temporary maintenance page is simple and effective.
  • All you need to redirect your visitors is the following code placed in your site’s root HTAccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
</IfModule>
click below button to copy the code. By Apache tutorial team

.htaccess features 8 : Redirect www to non-www

Some people prefer to use www.wikitechy.com, but some people prefer the shorter wikitechy.com. There isn’t really a right or wrong way to do it, but whatever you choose you can make sure all of your visitors get sent to the same place. With a few simple rules on the server you can choose from non-www to www, or redirecting from www to non-www.

rewriterule htaccess :

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www. wikitechy.com [NC]
RewriteRule ^(.*)$ http://wikitechy.com/$1 [L,R=301]
click below button to copy the code. By Apache tutorial team
Just replace wikitechy.com with your website and you can put this code in .htaccess file in your apache web server,

.htaccess features 9 : Redirect non-www to www

rewriterule htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ wikitechy.com [NC]
RewriteRule ^(.*)$ http://www. wikitechy.com/$1 [L,R=301]
click below button to copy the code. By Apache tutorial team
  • Just replace wikitechy.com with your website and you can put this code in .htaccess file in your apache web server,
  • Both of these rules send a full search engine friendly 301 HTTP redirect. They also preserve the entire URL (so yoursite.com/about redirects to www.yoursite.com/about).

.htaccess features 10 : Visitor IP Banning

<Limit GET POST>
order allow,deny
deny from 42.12.5.34
deny from 193.110.145.185
deny from 212.173.53.
deny from 69.242.
allow from all
</Limit>
click below button to copy the code. By Apache tutorial team

.htaccess features 11 : Disable Hot Linking

The below htaccess code is used to disable hot linking of common file types from other sites, so only your own domain(s) are allowed to reference/ access them. For example, by disabling hotlinking on .gif files, any site not within the allowed list of domains will get a broken image when they try to reference a .gif file that’s on your server

rewriterule htaccess :

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wikitechy.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)? cheap-hosting.wikitechy.com.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|png|js|css)$ - [F]
click below button to copy the code. By Apache tutorial team
Just replace wikitechy.com with your website and you can put this code in .htaccess file in your apache web server,

.htaccess features 12 : Prevent viewing of .htaccess file

  • If you use htaccess for password protection, then the location containing all of your password information is plainly available through the htaccess file.
  • If you have set incorrect permissions or if your server is not as secure as it could be, a browser has the potential to view an htaccess file through a standard web interface and thus compromise your site/server.
  • However, it is possible to prevent an htaccess file from being viewed in this manner:
<Files .htaccess>
order allow,deny
deny from all
</Files>
click below button to copy the code. By Apache tutorial team
The first line specifies that the file named .htaccess is having this rule applied to it. You could use this for other purposes as well if you get creative enough.

.htaccess features 13 : htaccess password - Protecting Files or Directories with .htaccess and .htpasswd

  • .htaccess and .htpasswd files - These two files work hand-in-hand to require a valid username and password when accessing a file; if no such password is provided, they simply display a “Restricted Access” error message and the invalid user is turned away.
  • This is the perfect way to protect
    • sensitive files,
    • administration areas,
    • sensitive login screens, from malicious hackers
  • htaccess htpasswd - The first step in this process is to create an .htpasswd file and place it into the directory where the protected file is located.
  • If it’s protecting a given directory, the .htpasswd file should be placed inside that protected directory.
  • The content of the file is really easy to create, as it contains a username and password separated by a colon. Here’s the basic setup:
ValidUser:hard2guessPassword
  • That single line of text goes into the .htpasswd file and that file is immediately uploaded to the server. If there should be more than one user and password combination that can grant access to the file or directory, they can be listed on separate lines with the .htpasswd document.
  • To create the authentication screen that requires one of the valid username and password combinations to be entered, site administrators need to add the following series of lines to their.htaccess file, requiring the password document and assigning it to a given directory or file:
AuthUserFile /server/path/of/.htpasswd
AuthType Basic
AuthName "The Title of the Protected Page Goes Here"
<Files "login.php">
Require ValidUser
</Files>
click below button to copy the code. By Apache tutorial team
  • All you needs to do is to point to the relevant .htpasswd file, specify the file or director being protected, and then give the page a title. That title will appear within the login form that is automatically generated by the browser when it encounters this restriction.

Related Searches to Features of .htaccess