.htaccess Reliable Tool

htaccess Reliable Tool

.htaccess is, as most know, a file that can control the behavior of the Apache Web server and make it possible to do a lot of interesting things with it. Most hosting providers offer the ability to use .htaccess files, but in the case of incorrect configuration it is very easy for your site to stop working, so I advise you to back up any changes.

Here are a couple of interesting lines of code that I would like to share with you and which I often needed in the various projects I worked on.

Remove Extensions from the URL

In case you want to remove .html or .php extensions from the URL, a little adjustment of .htaccess is a great way to do this and execute it.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Personalize the Error Message Pages

If you get bored with the standard Error 404 or Error 403 pages, there is a very simple way to set something that you like.

ErrorDocument 400 badrequest.html
ErrorDocument 401 authreqd.html
ErrorDocument 403 forbid.html
ErrorDocument 404 notfound.html
ErrorDocument 500 servererr.html

Forge Download of the Appropriate File Types

You do not want your visitors to open mp3 or video files within your browser? No problem.

 ForceType application/octet-stream
  Header set Content-Disposition attachment

Another way to do this is by changing the httpd.conf file:

<filesmatch "\.(?i:mp3)$"="">
  ForceType audio/mpeg
  SetEnvIf Request_URI "^.*/?([^/]*)$" FILENAME=$1
  Header set Content-Disposition "attachment; filename=%{FILENAME}e"

Prohibit Hot-Linking

If you do not want to unnecessarily load the server or spend the flow, you simply prohibit hot-linking. For example, the following code prohibits hot-linking images:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#change denied.jpg with your picture
RewriteRule .*\.(jpe?g|gif|bmp|png)$ denied.jpg [L]

Prevent the Contents of the Folders

Apache offers the ability to view the contents of a complete directory as well as navigate through directories. If you do not want everyone to see what you hold on a server, you can prevent it by either creating a blank index.html file or using the following code:

Options -Indexes

Delete or Add www to URL

If you want to redirect the user to the www or non-www version of your site, depending on what your primary domain is, you can easily and conveniently do this job with .htaccess.

Redirection to www:

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

Redirection to Non-www:

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

If you have some interesting code that could be added to this list, send it to me and I will gladly publish it. This can make it much easier to gather knowledge in one place and to help people with whom our shared knowledge and experience can be of use.

8 0
Article Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *