Things to know about .Htaccess
Learn the basics to .htaccess and why it is essential in order to get any content management system up and running.
Learn the basics to .htaccess and why it is essential in order to get any content management system up and running.
The .Htaccess file is very important for any database driven website, you can also use the file for basic static pages as well.
With .htaccess you can change the file extension on a certain file (index.php or index.html can be changed to index.yon).
To make a .htaccess file you must save a file as .htaccess, do not close the editor as the file is hidden and may be hard to find.
At the top of the file place:
Options +FollowSymlinks
RewriteEngine on
The above code tells the server that you will be editing already established names.
You can change the file extension with:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]
The above code allows you to change you file extension of .htm to .php, even though the file is not a php file but a .htm file.
Every wonder how blogs like wordpress hide the file extension and display a forward slash?
To do this is quite simple:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/$ $1.php [nc]
The above code will turn http://example.com/index.php into http://example.com/index/
The code below will be able to shorten a long url, for example:
Http://example.com/fileFolder/fileFolder2/file.php?id=4567890 to
Http://example.com/file1/
As you can seen this feature is very useful, here is the code:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]
This piece of code can prevent annoying website owners show images being hosted by you,
this can be very annoying as it can effect you data usage.
The term is called hot-linking
Here is the code:
Options +FollowSymlinks
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?corz\.org/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule .*\.(gif|jpg|png)$ http://corz.org/img/hotlink.png [NC]
Every wonder how to set custom error pages like 404 or 500?
Well below is the solution:
ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html
I will add more articles on .htaccess as it can be very useful.
Please note that if any of the above code does not work, check your host to see if they allow RewriteEngine to be on.
You are guaranteed to find a forum somewhere discussing .htaccess.
You can use our forum, simply add your topic to start the forum.