Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,877 members, 7,802,826 topics. Date: Friday, 19 April 2024 at 10:43 PM

Using The .htaccess file to improve your website - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Using The .htaccess file to improve your website (1643 Views)

How Do I Convert Csv File To Graph / Website Showing Error 403 After Unzipping File To File Manager / Htaccess File Tutorial And Tips (2) (3) (4)

(1) (Reply) (Go Down)

Using The .htaccess file to improve your website by Nobody: 4:47pm On Feb 23, 2016
You may have folders on your server you don't want people to see or locate. Normally if your users enter the folder name in the browser, an ugly file directory page shows up if you don't have any index files there, thus users can download content stored in that folder. Annoying isn't it?
A picture of the default error page is shown below, and a custom made error page for Rango Studios is also shown.

But there's this mysterious kind of file called .htaccess, it controls some user experience and gives you just enough power to keep your website healthy. The .htaccess file is mysterious because it is invincible, most servers have been configured to ignore them by default and some operating systems will hide them because of the '.' at the beginning of the file name.


What are .htaccess files?
Simply put, they are text files where server directories are stored. Unlike the master directive files like php.ini or httpd.conf, .htaccess directives apply only to the folder in which it lives and sub-folders too and because we can place multiple .htaccess files in any folder and also inherit properties from the parent .htaccess file we can build a nice chain of server directives.


What can .htaccess files be used for
Denying/Allowing access - Banning a specific IP from accessing your site, limiting specific special features to only your IP, Chasing off spam bots, amongst many other denial/allowance features.
ErrorDocument Redirection - Redirecting user to custom error pages, hiding folders and filetypes
Password Protection - Protecting folders you want only authorized people or admin to access
Bandwidth compression - reducing website bandwidth by outputting data through the php module
Pretty Linking - Making your site look neater by removing file extensions. e.g: www.rangostudios.com/register.php becomes www.rangostudios.com/register .
And a lot of other features i can't just go through everything.


In this short tutorial, i will show you few things that can be done with the .htaccess file.


ErrorDocument Redirection
This is how most people redirect you to a custom page when you type rubbish in your browser's address bar or you try to access a hidden folder.
for example, navigate to https://www.nairaland.com/ifoundshit or http://onhax.net/stupidlinks .


To do what you saw in the examples:
Create the
.htaccess
file in the root directory (where your website's files and folders are stored) or the public_html folder by
Opening any text editor, (notepad can be used too but make sure you remove the .txt extension when saving) and saving as .htaccess in the above stated directory.

Note: you need to be running a web server to use the .htaccess fille ordinary html pages on your computer without a web server will show you a file not found error instead when a wrong value is inputted into your browser address bar.

Put the following code in your .htaccess file if you are on a live server:


# custom error documents
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php

Where /error refer to your defined error folder (holding your custom error page) which is in the root directory too.

Put the following code in your .htaccess file if you are on a localhost server:

# custom error documents
ErrorDocument 401 site/error/401.php
ErrorDocument 403 site/error/403.php
ErrorDocument 404 site/error/404.php
ErrorDocument 500 site/error/500.php
[/code]

Denying/Allowing access:

To ban a specific IP,

order allow,deny
deny from 88.123.22.419
allow from all

The user with the IP 88.123.22.419 will get the 403 access denied error. General IP rules can be applied here, so you can ban users from specific countries. Now you see how some US sites get you off their ass.

Hiding all folders from being browsed by users:

It's just takes one line to do this

Index Ignore * #Users will get to the page but see nothing

or

Options -Indexes #to totally hide all root folders and throw an error document to users

or

# deny all .htaccess, .DS_Store $hî†é and ._* (resource fork) files
<Files ~ "^\.([Hh][Tt]|[Dd][Ss]_[Ss]|[_])">
Order allow,deny
Deny from all
Satisfy All
</Files>
# deny access to all .log and .comment files
<Files ~ "^.*\.([Ll][Oo][]|[cC][oO][mM][mM][eE][nN][tT])">
Order allow,deny
Deny from all
Satisfy All
</Files>

and also

# removing user session from url
php_value session.use_trans_sid 0


Bandwidth Compression:
For people that pay for bandwidth, you know what it means to compress them. It saves money!
With a few lines of code you can save cool cash on bandwidth.

<ifModule mod_php4.c>
php_value zlib.output_compression 16386
</ifModule>


There are many other things that can be done as i said earlier, like you can also use the .htaccess file to retain your website's traffic rank when you change domains, e.t.c.

Pretty Linking is pretty easy, so i will leave that as an assignment. Do your research on it.

Note: People following the picture examples containing code precisely the second picture, should leave an empty line at the end of the code. If your code typically ends on line 4, add an empty line. This is to get rid of frustrating server issues in the future.

1 Like

Re: Using The .htaccess file to improve your website by nnamdiosu(m): 6:22pm On Feb 23, 2016
nice one Daniel. but pls u didn't talk about using httacess to hide URL extensions

1 Like

Re: Using The .htaccess file to improve your website by Nobody: 6:57pm On Feb 23, 2016
nnamdiosu:
nice one Daniel. but pls u didn't talk about using httacess to hide URL extensions

I'm happy you found this useful, I'm currently out and on a tablet phone. I will address the skipped part when I'm back home. (Probably in some few minutes).

Update
I just went through my original post and remembered that I said at the end, the short linking feature is research task for you guys. If you insist, I shall explain it.
Re: Using The .htaccess file to improve your website by nnamdiosu(m): 8:11pm On Feb 23, 2016
DanielTheGeek:


I'm happy you found this useful, I'm currently out and on a tablet phone. I will address the skipped part when I'm back home. (Probably in some few minutes).

Update
I just went through my original post and remembered that I said at the end, the short linking feature is research task for you guys. If you insist, I shall explain it.


no. pls. I accept the challenge. I'll post something on it tomorrow then

1 Like

Re: Using The .htaccess file to improve your website by Nobody: 8:14pm On Feb 23, 2016
nnamdiosu:



no. pls. I accept the challenge. I'll post something on it tomorrow then

Okay, good luck.
Re: Using The .htaccess file to improve your website by Nobody: 9:38pm On Feb 23, 2016
'trolling. . . .
Re: Using The .htaccess file to improve your website by nnamdiosu(m): 10:39pm On Feb 24, 2016
RewriteRule ^about$ about.php [L]







That little bit will make http://example.com/about.php be able to load at http://example.com/about
thanks daniel

1 Like

Re: Using The .htaccess file to improve your website by Nobody: 11:42pm On Feb 24, 2016
nnamdiosu:
RewriteRule ^about$ about.php [L]







That little bit will make http://example.com/about.php be able to load at http://example.com/about
thanks daniel

You are welcome.

Also try researching about the .htpasswd file and how to use it with .htaccess

(1) (Reply)

Google And Udacity Launches Android Nanodegree To Teach Coding / Please Help, Browsers Can't Find My External Css / who Can Help Me With Countries State And Cities Sql

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 25
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.