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: However, if you want to enable directory browsing, change it to the following:.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: 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
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.
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,.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:
.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 :
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 :- 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
.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 serverrewriterule htaccess :
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:
.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:
- 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:
- 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.