.htaccess Tips and Tricks for Beginners

The .htaccess file can prove to be a valuable ally in controlling access to your server's resources or if you want the space you have rented on a server, since it allows you to secure your server with just a few lines of code bypassing any management applications you have, which usually have a lot of work to do and are basically overloaded.

Let's see how

html-code .htaccess

If you need information about what a file is .htaccess, you can read related articles at Wikipedia which are quite illustrative.

Let's see some useful .htaccess tricks that you will definitely be interested in όλα.

Redirecting users to custom error pages

Instead of viewing the default Apache, Lightspeed, or Nginx bug pages, webmasters can redirect these requests into their own custom text.

This can be a text file, a HTML, a Perl script, or whatever else you want, as long as the server is able to read your text and send it to the user (in the examples below we redirect them to our own html pages).

 
ErrorDocument 401 /error_pages/401.html ErrorDocument 403 /error_pages/403.html ErrorDocument 404 /error_pages/404.html ErrorDocument 500 /error_pages/500.html

Make sure pages with www and without www show the same thing
"Www" is no longer mandatory on URLs, but there are times when WWW and non-WWW versions of a website may lead to different locations. This could be due to a poor server setup, incorrect DNS settings, or faulty hosting accounts. To avoid this situation, save the following text to the .htaccess file and it will resolve the issue

 
## RECONSTRUCTION WITHOUT -WWW to WWW RewriteEngine On RewriteCond% {HTTP_HOST} igur iguru \ .gr RewriteRule (. *) Https://iguru.gr/$1 [R = 301, L]

To do this, mod_rewrite must first be enabled on your server, and you must also make sure that the "RewriteEngine On" bar is present somewhere in your .htaccess file. We have already included it in the code in case you have not already activated it.

The reverse process is the following (ie the redirection of:

 
## REDUCTION OF WWW WITHOUT-WWW RewriteEngine On RewriteCond% {HTTP_HOST} ^ www \ .iguru \ .gr
RewriteRule (. *) Https://iguru.gr/$1 [R = 301, L]

Note: be sure to change the name of our isotope to yours otherwise you will see the clicks to your site decrease dramatically !!!!!!!! :)

Send users to a subdomain
Sometimes you may want to send your users to a custom subdomain. This can be done easily and quickly using .htaccess.

 
RewriteEngine On RewriteCond% {HTTP_HOST}! ^ $ RewriteCond% {HTTP_HOST}! ^ Subdomain \ .domain \ .com $ [NC] RewriteRule ^ / (. *) $ Http://subdomain.domain.com/$1 [L, R = 301]

HTTP to HTTPS redirects
It is not that simple. Redirecting users to the HTTPS version of your site does not automatically make it "safe". We need to look at the SSL application for this.

 
RewriteEngine On RewriteCond% {HTTPS} off RewriteRule (. *) Https: //% {HTTP_HOST}% {REQUEST_URI}

Multimedia power files to download to the user's browser
Some browsers (or if you prefer now almost all) tend to force them to be opened by some internal application instead of allowing users to simply download them. To make sure that the file is stored locally each time (ie on the user's computer), and is not continuously streamed to the user, which "eats" , this trick can help:

 
AddType application / octet-stream .pdf AddType application / octet-stream .avi AddType application / octet-stream .mp3

You can declare any type of file extension you like. The server will force it to download it to the user.

Protect your files from hotlinking
Yes, it's so simple to protect your files from the hotlinked Web. You may think that this protection requires complicated PHP solutions or JavaScript-based solutions, money to pay developers, a lot of time to add each file to various firewalls and tables, but no, they only need these three lines code.

 
RewriteEngine On RewriteCond% {HTTP_REFERER}! ^ RewriteCond% {HTTP_REFERER}! ^ Http: // (www \.)? Iguru \ .gr / [nc] RewriteRule. * \. (Gif | jpg | png) $ https: / /iguru.gr/bullshit_hotlink_image.png [nc]

Text compression, HTML, JavaScript, CSS, and XML code
The following snippet is for Apache servers and will automatically compress HTML, JavaScript, CSS, and XML files when it sends them to your users.

 

 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/x-component
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript
 

Disable list browsing on any webpage
Regardless of using the .htaccess file in WordPress, Drupal, Joomla, or with Java, Python, or Ruby, the following line of code in your .htaccess file will prevent any user from exploring your site's directories and public view files stored inside them.

 
Options-Indexes

Disable PHP execution within a folder or for a specific file
Using the .htaccess file filtering capabilities, webmasters can target PHP files (HTML, JavaScript, images, etc.), and then "do something" about them. Because webmasters generally want to block access using .htaccess files, this short snippet can be quite useful.

 
#ΟΛΑ ΤΑ PHP ΑΡΧΕΙΑ
 
 deny from all
 
 #ΚΑΠΟΙΟ ΣΥΓΚΕΚΡΙΜΕΝΟ PHP ΑΡΧΕΙΟ
 
 deny from all
 
 

Do not allow access to one or more IP addresses
If there are only a few users who tend to abuse your service, you do not need to apply a firewall rule just for them. This can be done in the .htaccess file like this:

 

 order allow,deny
 #ΑΠΑΓΟΡΕΥΣΗ (BAN) ΜΙΑΣ IP
 deny from xxx.xxx.xxx.xxx
 #ΑΠΑΓΟΡΕΥΣΗ (BAN) ΟΛΟΚΛΗΡΗΣ ΚΑΤΗΓΟΡΙΑΣ ΑΠΟ IPS
 deny from xxx.xxx.xxx.xxx/24
 allow from all 

Note: where xxx corresponding numbers of IPS you want to throw out

Of course it is not necessary to use the .htaccess file so regularly and for all the above cases. All of these code snippets can also be used in your httpd.conf, as the .htaccess file should only be used in cases where directory permissions are required and access to httpd.conf is not allowed or is not possible.

iGuRu.gr The Best Technology Site in Greecefgns

every publication, directly to your inbox

Join the 2.087 registrants.

Written by Dimitris

Dimitris hates on Mondays .....

Leave a reply

Your email address is not published. Required fields are mentioned with *

Your message will not be published if:
1. Contains insulting, defamatory, racist, offensive or inappropriate comments.
2. Causes harm to minors.
3. It interferes with the privacy and individual and social rights of other users.
4. Advertises products or services or websites.
5. Contains personal information (address, phone, etc.).