Stats: 3,183,638 members, 7,921,381 topics. Date: Thursday, 15 August 2024 at 12:53 AM |
Nairaland Forum / Science/Technology / Programming / Using The .htaccess file to improve your website (1770 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)
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 .htaccessfile 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:
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,
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
or
or
and also
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.
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: 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: 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: 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: You are welcome. Also try researching about the .htpasswd file and how to use it with .htaccess |
(1) (Reply)
Dhtmlframework Is Going Open-Source! / How To Create A Login Form Using Html, Css, Javascript, Php And Ajax / Tutorial On Java Micro Edition, Very Urgent!
(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. 39 |