Bruno Kitsune Website
Sponsored Ad

Hello friends. Did you know that means .htaccess Hypertext Access and can be used on any Apache server? With it you will may place a more friendly URL, change the index of your folder, create block against hotlink, force the use or absence of www, among many other possibilities!

Well, to take advantage of this short tutorial, you need a website and an Apache server.

Generating the .htaccess

To do this is simple. Open Notepad and place the content you want into it. Save the file. Name it .htaccess and the file type to "All Files".

After saving just send this file to a folder of your site, the changes are applied to all the files and folders within the folder that you sent. For example: I have sent my .htaccess inside the main folder "/", so the changes are in effect throughout the site. If I send to "/downloads", this file changes are valid for "/downloads" and all the folders within it. You can have one for "/" and one for "/downloads", sent to the "/" makes the rules are used in all folders except "/ downloads".

Error pages

The error pages appear when the web server is something that should not be happening, for example, a page does not exist, or you do not have permission to open the folder, etc.

You can make your error page as desired. Then send them to your server.

Examples of errors:

  • 400 - Syntax error
  • 401 - Required username and password that were entered incorrectly.
  • 402 - Locked Content for paying users or private directory.
  • 403 - No permission.
  • 404 - File not found.
  • 500 - Server with internal errors.
  • 501 - The requested service can not be included in the server.
  • 502 - Overloaded server.
  • 503 - The server gateway did not respond to your request.

Then place the code below into your .htaccess.

ErrorDocument 400 /error400.html
ErrorDocument 401 /error401.html
ErrorDocument 402 /error402.html
ErrorDocument 403 /error403.html
ErrorDocument 404 /error404.html
ErrorDocument 500 /error500.html
ErrorDocument 501 /error501.html
ErrorDocument 502 /error502.html
ErrorDocument 503 /error503.html

In this example, I sent the error pages in the same folder where I'll put the .htaccess, hence the reference is just "/" if you had sent the error pages to the folder "/error" for example, the references would be "/error/errorXXX.html".

If you want, you can use an absolute address (http://rctbr.com/~bk/error404.html), rather than a relative (/error404.html) but I would not recommend.

Nota: Você pode dar qualquer nome para as páginas de erro, não precisa seguir o padrão erroXXX.html.

Rewrite Engine

The rewrite engine is a feature that allows you to change directories or rules, you need to activate it to continue with the tutorial. To do this, add the line above all RewriteCond and all Rewrites Rules (which you'll see below).

RewriteEngine on

Tips:

  • Place the first line of the document (just activate once for the entire document).
  • Some servers do not accept the Rewrite Engine.

The use of  www

A few years ago, it was necessary defirinir names protocols and resources to be able to load a website. The use of the www (World Wide Web) was required to do so.

Over the years, the technology could leave many automated things and the protocol (note that it is protocol and not the total use) of the www is unnecessary for the vast majority of servers.

It is up to you, webmaster, define what is best for your site.

Forcing the use of www

The Rewrite engine must be turned on!

Many web-masters like or even ordering their use. If you are one of them and want to force use of Web page, simply add the code below into a blank line in the .htaccess.

Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,R=301]

Forcing not using www

The Rewrite engine must be turned on!

This causes users who type www.exemple.com be redirected directly to exemple.com.

Here is the content to put in .htaccess.

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Hotlink protection

The Rewrite engine must be turned on!

Many sites have a huge bandwidth for other sites directly indicate its contents, redirecting images, files, etc.

You, as a mentor and web-master may feel offended by this and may want to protect your files from indiscreet glances. After all, it costs nothing to the other web-master link a page (http://example.com/me.html) instead of direct content (http://example.com/me/creation.exe).

So let's content to put in .htaccess.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?brunokitsune\.com/ [NC] 
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?rctbr\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(gif|jpg|jpeg|bmp|png|mp3|wma|wmv|avi|mpg|mpeg|zip|rar|7z|exe)$ http://example.com/~bk/hotlink.jpe [R,NC,L]

Understanding this content:

The lines with website addresses indicate the pages that can make hotlink to my site. The line RewriteCond% {HTTP_REFERER}! ^ $ Means all the others will be blocked.

The Rewrite Rule has two notable indications:

  1. Locked extensions for download (gif | jpg | jpeg | bmp | png | mp3 | wma | wmv | avi | mpg | mpeg | zip | rar | 7z | exe).
  2. When the diversion is done, the person will see this content (http://example.com/~bk/hotlink.jpe)

The correct thing hotlink.jpeg, right? Wrong! If you put .jpeg, the image is blocked by your hotlink rule! Placing a letter unless the image extension, it is not caught by the rule. If you want you can host the image on a site with this function, then it would be possible to do this because the file is on a server that accepts hotlink.

It is worth noting that this rule can have the reverse, ie, "Block only a few sites and allow the rest." How to do this? Simple! In the following example, let's imagine that I want to block two websites: Facebook and BlogSpot.

RewriteCond %{HTTP_REFERER} !^https://(.+\.)?facebook\.com/ [NC,OR] 
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?blogspot\.com/ [NC]
RewriteRule .*\.(gif|jpg|jpeg|bmp|png|mp3|wma|wmv|avi|mpg|mpeg|zip|rar|7z|exe)$ http://example.com/~bk/hotlink.jpe [R,NC,L]

You may have noticed two changes: RewriteCond% {HTTP_REFERER} ^ $ is gone and now we are using the OR criteria.

Friendly URL

The Rewrite engine must be turned on! Some sites have difficult access address, especially when working with PHP, for example, let's imagine that the page exists below:

http://example.com/~bk/content.php?action=show

Through the system of URL friendly, we can transform this into:

http://example.com/~bk/show

More simple, no? How do we do this?

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/?$ /content.php?action=$1

The -d -f indicate that should not be considered files and directories. The last line is a redirection rule, in this case, o $1 means a variable. For more information, visit this link.

Changing the index

Often you are sending a surprise to index your site and do not want anyone looking in advance. We can force the user to see another "index" to access your site (http://example.com/~bk/).

DirectoryIndex construction.html

By doing this, the index becomes construction.html. You could also use a folder with content, like this:

DirectoryIndex /construction/index.html

I hope I have collaborated with you in the best possible way if you have any questions please contact me.

Sponsored Ad