Vous êtes sur la page 1sur 12

Htaccess: Introduction - Uniform Server Wiki System Página 1

Htaccess: Introduction
From Uniform Server Wiki System

.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |

Contents
[hide]

1 Whats in a name .htaccess - Apache directory-level configuration file


2 Create the file
3 Several htaccess files
4 What it affects
5 What can you do with it?

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.

Create the file


The easiest way to create the .htaccess file is just to copy an exiting one into its new location and edit its content. The problem in creating a
new file from scratch is that most windows text editors (Window Notepad is no exception ) will insist on adding a dot txt extension so the file
looks like this .htacces.txt even trying to rename this file Windows XP insists on a file name.

Note: Most commands in htaccess are meant to be placed on single line only.

Several htaccess files


You will find several htaccess files in Uniform Server do not edit these they are part of Uniform Server's security. That said the one contained
in root folder WWW is placed there for you to edit, it allows you to put your server online. You can copy this file into any folder you create
within your website and edit it to meet your needs.

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.

http://wiki.uniformserver.com/index.php/Htaccess:_Introduction 05-11-2010 15:50:49


Htaccess: Introduction - Uniform Server Wiki System Página 2

What can you do with it?


Uniform Server places no restrictions on what can be contained in the htaccess file. Hence htaccess examples found on the Internet or from
textbooks should work. I have included a few to get you started.

Top

Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Introduction"

Categories: UniCenter | Support | Troubleshooting | Application | Development

http://wiki.uniformserver.com/index.php/Htaccess:_Introduction 05-11-2010 15:50:49


Htaccess: Site error documents - Uniform Server Wiki System Página 1

Htaccess: Site error documents


From Uniform Server Wiki System

.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

Site error documents

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.

Whats errors produce what


The following are a list of the Apache error numbers and what they relate to:

Successful Client Requests Client Request Errors Server Errors


201 Created 400 Bad Request 500 Internal Server Error
202 Accepted 401 Authorization Required 501 Not Implemented
203 Non-Authorative Information 403 Forbidden 502 Bad Gateway
204 No Content 404 Not Found 503 Service Unavailable
205 Reset Content 405 Method Not Allowed 504 Gateway Timeout
206 Partial Content 406 Not Acceptable (encoding) 505 HTTP Version Not Supported
Client Request Redirected 407 Proxy Authentication Required
300 Multiple Choices 408 Request Timed Out
301 Moved Permanently 409 Conflicting Request
302 Moved Temporarily 410 Gone
303 See Other 411 Content Length Required
304 Not Modified 412 Precondition Failed
305 Use Proxy 413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type

Note: There is no need to create error pages for all the above, following are worth considering: 401, 403, 404 and 500

Top

How to create customised error documents


Create a new folder in your root folder (www) and name it errors. This folder contains all the error pages you wish to display. For example
not_found.html this will be displayed when a page cannot be found on your web site.

Now open the root htaccess file (the one in your root folder www) and add the following line:

ErrorDocument 404 /errors/not_found.html

http://wiki.uniformserver.com/index.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57


Htaccess: Site error documents - Uniform Server Wiki System Página 2

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:

ErrorDocument 401 /errors/auth_reqired.html


ErrorDocument 403 /errors/forbidden.html
ErrorDocument 404 /errors/not_found.html
ErrorDocument 500 /errors/server_error.html

Or look like similar to this if when using full URL paths:

ErrorDocument 401 http://yoursite.com/errors/auth_reqired.html


ErrorDocument 403 http://yoursite.com/errors/forbidden.html
ErrorDocument 404 http://yoursite.com/errors/not_found.html
ErrorDocument 500 http://yoursite.com/errors/server_error.html

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>

Add the command to htaccess:

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

# To allow execution of cgi scripts in this directory uncomment next


1. Navigate to the root folder www. two lines.
2. Open the .htaccess file
3. Add the command shown on the right AddHandler cgi-script .pl .cgi
last line. Options +ExecCGI
4. Save the file
# To unlock your server, comment the next 4 lines.
# Defaults: Username = admin; Password = userver

#AuthName "Uniform Server - Secure Server Access"


#AuthType Basic
#AuthUserFile /htpasswd/www/.htpasswd
#Require valid-user

ErrorDocument 404 /errors/not_found.html

Testing
1. Run the server using Server_Start.bat

http://wiki.uniformserver.com/index.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57


Htaccess: Site error documents - Uniform Server Wiki System Página 3

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

Experiment with the ErrorDocument command and create a few pages.

Top

Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Site_error_documents"

Categories: UniCenter | Support | Troubleshooting | Application | Development

http://wiki.uniformserver.com/index.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57


Htaccess: Prevent Directory Listing - Uniform Server Wiki System Página 1

Htaccess: Prevent Directory Listing


From Uniform Server Wiki System

.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

Prevent Folder (Directory) listing

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

index.html, index.shtml, index.html.var


index.htm, index.php3, index.php
index.pl, index.cgi

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

Personalise index page listings


If you are not going to prevent folder listings consider personalising the page displayed.

http://wiki.uniformserver.com/index.php/Htaccess:_Prevent_Directory_Listing 05-11-2010 15:51:05


Htaccess: Prevent Directory Listing - Uniform Server Wiki System Página 2

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:

File name Comment

This is just a text file containing something like this:

HEADER.html <h1>Power of htaccess</h1>

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:

<h1>More Power of htaccess</h1>'''


README.html
<p>Why! The name README and not FOOTER A have no idea</p>

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"

Categories: UniCenter | Support | Troubleshooting | Application | Development

http://wiki.uniformserver.com/index.php/Htaccess:_Prevent_Directory_Listing 05-11-2010 15:51:05


Htaccess: Redirect - Uniform Server Wiki System Página 1

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:

Redirect old_path new_path

File Redirect format

Looking at this in a little more detail using this example

Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html

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:

Redirect /olddirectory http://yoursite.com/newdirectory

1. Redirect

http://wiki.uniformserver.com/index.php/Htaccess:_Redirect 05-11-2010 15:51:11


Htaccess: Redirect - Uniform Server Wiki System Página 2

This is the Apache command followed by a space


2. /olddirectory
This is the path to the old directory you want redirected
It is stated in a relative file format
Again followed by a space
3. http://yoursite.com/newdirectory
This is the full (URL) path of the location you want the request redirected to

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

# To allow execution of cgi scripts in this directory uncomment next


1. Navigate to the root folder www. two lines.
2. Open the .htaccess file
3. Add the command shown on the right AddHandler cgi-script .pl .cgi
last line. Options +ExecCGI
4. Save the file
# To unlock your server, comment the next 4 lines.
# Defaults: Username = admin; Password = userver

#AuthName "Uniform Server - Secure Server Access"


#AuthType Basic
#AuthUserFile /htpasswd/www/.htpasswd
#Require valid-user

Redirect /old http://localhost/moved

Testing

1. Run the server using Server_Start.bat


2. In the browser address bar type http:/localhost/old
3. You will be redirected to the folder http://localhost/moved/

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

1. Run the server using Server_Start.bat


2. In the browser address bar type http:/localhost/old/not_found.html
3. You will be redirected to the file http://localhost/moved/not_found.html

Well you get the idea experiment with files it is similar to the folder example.

Top

Practical Example

http://wiki.uniformserver.com/index.php/Htaccess:_Redirect 05-11-2010 15:51:11


Htaccess: Redirect - Uniform Server Wiki System Página 3

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

Redirect /u35_mod_ssl_2/4_hmailserver.html http://wiki.uniformserver.com/index.php/SSL_Part_2:_Installing_


hMailServer
Redirect /u35_mod_ssl_2/5_hmailserver.html http://wiki.uniformserver.com/index.php/SSL_Part_2:_Configure_
hMailServer

Redirect /new_users/new_users_index.html http://wiki.uniformserver.com/index.php/New_Users:_Home


Redirect /new_users/new_users_install_1.html http://wiki.uniformserver.com/index.php/New_Users:_Quick_Install_
Guide
Redirect /new_users/problems_1.html http://wiki.uniformserver.com/index.php/New_Users:_Problems_Section

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"

Categories: UniCenter | Support | Troubleshooting | Application | Development

http://wiki.uniformserver.com/index.php/Htaccess:_Redirect 05-11-2010 15:51:11


Htaccess: Preventing hot linking - Uniform Server Wiki System Página 1

Htaccess: Preventing hot linking


From Uniform Server Wiki System

.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:

Serve a broken image


This code produces a broken image to be displayed when its hot linked. Make sure to replace "mydomain.com" with your own.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www1. ) ?mydomain. com/ . *$ [ NC]
RewriteRule \ . ( gif | jpg ) $ - [ F]

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]

Link: I think the original source came from here: webmasterworld.com

Serve an alternative image


I keep telling you its your server and you can do what you like so why not server an alternative image in your favor again remember to replace
"mydomain.com" with your own:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.mydomain.com/nasty.gif [R,L]

General note

http://wiki.uniformserver.com/index.php/Htaccess:_Preventing_hot_linking 05-11-2010 15:51:17


Htaccess: Preventing hot linking - Uniform Server Wiki System Página 2

I have seen several cases where mod rewrite is used in htaccess files and the line

Options +FollowSymLinks

is missing which effectively informs Apache to ignore mod_rewrite.

This error shows up in the log files as:

[client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: W:/
www/textpattern/

Apache log files are located in folder: *\Uniform Server\udrive\usr\local\apache2\logs

Top

Ric
Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Preventing_hot_linking"

Categories: UniCenter | Support | Troubleshooting | Application | Development

http://wiki.uniformserver.com/index.php/Htaccess:_Preventing_hot_linking 05-11-2010 15:51:17

Vous aimerez peut-être aussi