Académique Documents
Professionnel Documents
Culture Documents
Htaccess: Introduction
From Uniform Server Wiki System
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |
Contents
[hide]
The .htaccess file is a simple ASCII file it provides an easy method to extend the Apache configuration file you edit this file with a text editor
such as NotePad never use a word processor to do this because they add extra formatting characters.
This section provides a detailed description of some commands you can use in this file a more concise and quick reference guide can be found
on this page HT.
Whats in a name
In Unix, files beginning with a period ( . ) typically hold settings for programs these are refered to as dot files. Apache is a Unix application
ported to Windows for cpmtatabilty retains this naming convention. For all Windows users the file .htaccess is just that a file; at first it looks
strange but that's it's full name it has no extension or anything before the dot.
Note: Most commands in htaccess are meant to be placed on single line only.
What it affects
The htaccess file affects the folder it is placed in and all sub-folders. The htaccess file located in your root folder (folder WWW) affects your
website and all folders and files below it.
You can tailor this action so it does not apply to a specific folder by copying the htaccess file to that folder and removing the htaccess
commands that you do not want to be applied to this folder and it’s sub-folders.
From the viewpoint of a folder or file it is the nearest htaccess file that affects it.
Top
Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Introduction"
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |
Contents
[hide]
.htaccess - Apache directory-level configuration file
1 Whats errors produce what
2 How to create customised error documents
3 Example
3.1 Testing
Apache provides some bland error documents however you are not restricted to using these you can provide your own and match them to your
sites look.
Note: There is no need to create error pages for all the above, following are worth considering: 401, 403, 404 and 500
Top
Now open the root htaccess file (the one in your root folder www) and add the following line:
Save the file. The command ErrorDocument 404 will forward a user to yoursite/errors/not_found.html file when ever Apache produces the
error 404.
Note 1: The files and folder can be named anything you like.
Note 2: You can use a full URL (as oppose to a virtual path) to your error file for example: http://yoursite.com/errors/not_found.html
If you create error pages for the above your htaccess file will look similar to this when using virtual paths:
Top
Example
The following shows a little more detail first create the error page to be displayed
Comment Code
<html>
1. Create a new folder in www named errors. <title>Not found</title>
2. In this folder create a file named not_found.html <body>
3. Add the code shown on the right to it. <p>Example of a not found page</p>
4. Save the file </body>
</html>
Comment Code
# This file provides security to the server limiting access to the
localhost only.
# Comment to deactivate.
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Testing
1. Run the server using Server_Start.bat
2. In the browser address bar type http:/localhost.fred.html or some other file that does not exist.
3. Apache will generate the error code 404 and redirect to your not_found.html page
Top
Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Site_error_documents"
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |
Contents
[hide] .htaccess - Apache directory-level configuration file
1 htaccess commands
2 Personalise index page listings
I find some times I use the term folder and at other times directory these are interchangeable and mean the same thing so forgive me when I do
this.
If you have read the [Htaccess: Site error documents | site error documents page]] you will have created a folder named error. Type the
following into your browser address bar : http://localhost/errors/ you will be greeted with a full listing of its content (folders and files).
Try it on any folder that does not contain any of the following pages
and you will receive a listing of its contents as would anyone on the internet.
You may or may not find this desirable this page shows you how to prevent this listing.
Top
htaccess commands
There is only a single command to learn:
Command Comment
IndexIgnore * This prevents listing of all the files, the * is a wildcard that matches all Syste
You can be selective and state the file types you do not want listed. Again the wildcard matches all files, in this
IndexIgnore *.gif *.jpg
example all gif and jpg image files are targeted and will not be displayed. While all others will be displayed.
Top
You can personalise the index pages listed by adding a header and footer. This requires either one of two files placed in the folder with the
htaccess file as follows:
Note: You can insert any regular HTML tag. They are not complete HTML pages just snippets that are included.
Again this is a text file that uses any regular HTML tag for example:
Note: You can insert any regular HTML tag. They are not complete HTML pages just snippets that are included.
Top
Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Prevent_Directory_Listing"
Htaccess: Redirect
From Uniform Server Wiki System
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |
Contents
[hide]
1 Redirect format
1.1 File Redirect format .htaccess - Apache directory-level configuration file
1.2 Folder Redirect
2 Directory example 1
3 Directory example 2
4 Practical Example
4.1 Errors
Redirect
The htaccess command Redirect is very powerful you can redirect files or complete folders. Why would you want to do this a typical example
would be that you have changed the structure of your site but wish old links (saved by a user) to be redirected to your new page location or
even new page name.
Redirect format
The redirect command must be on a single line with each element separated by a space it has the following format:
1. Redirect
This is the Apache command followed by a space
2. /olddirectory/oldfile.html
This is the path to the old file you want redirected
It is stated in a relative file ...tem
Again followed by a space
3. http://yoursite.com/newdirectory/newfile.html
This is the full (URL) path of the location you want the request redirected to
You would place a Redirect command in the root htaccess file for each file you want to redirect.
Folder Redirect
Suppose that all you have done is renamed a folder (and even moved it to another location) however all the files still have their original names
redirecting a folder is similar to the above for example:
1. Redirect
Top
Directory example 1
Using our errors folder make a copy of it and rename it to moved.
Comment Code
# This file provides security to the server limiting linkingto the
localhost only.
# Comment to deactivate.
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Testing
Top
Directory example 2
Open the file not_found.html contained in folder moved add some more text to distinguish it from the original.
Testing
Well you get the idea experiment with files it is similar to the folder example.
Top
Practical Example
I was faced with this very problem UniCenter has been around for nearly two years and started to become unmanageable hence the move over
to the Wiki. Many web sites link to various pages on UniCenter to avoid frustration of not finding an article; users are redirected to the
appropriate page on the Wiki.
Below is a small extract from my .htaccess file you will see I am using page redirection:
Redirect /us_35_apps/joomla/joomla_install_1.html http://wiki.uniformserver.com/index.php/Installing_Joomla
Redirect /us_35_apps/joomla/joomla_install_2.html http://wiki.uniformserver.com/index.php/Joomla_Portability
Tip: I first publish the Wiki pages, use a browser to view a page and copy the link into .htaccess. Similar process to obtain the redirected
page.
Errors
Any typos in the .htaccess file will cause Apache to spit out the following error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Hence always make small changes to .htaccess and then test, makes it easier to isolate and resolve any problems.
Top
Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Redirect"
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |
Contents
[hide]
.htaccess - Apache directory-level configuration file
1 Serve a broken image
2 Serve an alternative image
3 General note
Hot linking
What is hot linking? Its when someone links to content on your server and uses your server to deliver the goods. It could be images or any
non html objects, they are effectively stealing your bandwidth at you expense.
Uniform Server has mod-rewrite enabled, allowing you to rewrite the requested URL on-the-fly based on configuration directives and rules.
Yep it as complex as it sounds. I found this code on the Internet it prevents hot linking to images on your site.
Just pop this code into your root htaccess file for global effect or a sub-folder to localise the effect to just one section of your site:
Or
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?(subdomain_mydomain\.)?mydomain.com/ [NC]
RewriteRule \.(jpe?g|gif|png|bmp)$ - [F]
General note
I have seen several cases where mod rewrite is used in htaccess files and the line
Options +FollowSymLinks
[client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: W:/
www/textpattern/
Top
Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Preventing_hot_linking"