12
Página 1 Htaccess: Introduction - Uniform Server Wiki System 05-11-2010 15:50:49 http://wiki.uniformserver.com/index.php/Htaccess:_Introduction 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 2 Create the file 3 Several htaccess files 4 What it affects 5 What can you do with it? .htaccess - Apache directory-level configuration file 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.

Htaccess - Uniform Server Wiki System

Embed Size (px)

Citation preview

Page 1: Htaccess - Uniform Server Wiki System

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

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

Htaccess: IntroductionFrom Uniform Server Wiki System

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

Contents[hide]

1 Whats in a name2 Create the file3 Several htaccess files4 What it affects5 What can you do with it?

.htaccess - Apache directory-level configuration file

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 editorsuch 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 foundon this page HT.

Whats in a nameIn Unix, files beginning with a period ( . ) typically hold settings for programs these are refered to as dot files. Apache is a Unix applicationported to Windows for cpmtatabilty retains this naming convention. For all Windows users the file .htaccess is just that a file; at first it looksstrange but that's it's full name it has no extension or anything before the dot.

Create the fileThe 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 anew file from scratch is that most windows text editors (Window Notepad is no exception ) will insist on adding a dot txt extension so the filelooks 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 filesYou will find several htaccess files in Uniform Server do not edit these they are part of Uniform Server's security. That said the one containedin 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 createwithin your website and edit it to meet your needs.

What it affectsThe htaccess file affects the folder it is placed in and all sub-folders. The htaccess file located in your root folder (folder WWW) affects yourwebsite 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 htaccesscommands 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.

Page 2: Htaccess - Uniform Server Wiki System

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

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

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 fromtextbooks should work. I have included a few to get you started.

Top

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

Categories: UniCenter | Support | Troubleshooting | Application | Development

Page 3: Htaccess - Uniform Server Wiki System

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

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

Htaccess: Site error documentsFrom Uniform Server Wiki System

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

Contents[hide]

1 Whats errors produce what2 How to create customised error documents3 Example

3.1 Testing

.htaccess - Apache directory-level configuration file

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 yoursites look.

Whats errors produce whatThe 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 documentsCreate a new folder in your root folder (www) and name it errors. This folder contains all the error pages you wish to display. For examplenot_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

Page 4: Htaccess - Uniform Server Wiki System

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

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

Save the file. The command ErrorDocument 404 will forward a user to yoursite/errors/not_found.html file when ever Apache produces theerror 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.htmlErrorDocument 403 /errors/forbidden.htmlErrorDocument 404 /errors/not_found.htmlErrorDocument 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.htmlErrorDocument 403 http://yoursite.com/errors/forbidden.htmlErrorDocument 404 http://yoursite.com/errors/not_found.htmlErrorDocument 500 http://yoursite.com/errors/server_error.html

Top

ExampleThe following shows a little more detail first create the error page to be displayed

Comment Code

1. Create a new folder in www named errors.2. In this folder create a file named not_found.html 3. Add the code shown on the right to it.4. Save the file

<html> <title>Not found</title> <body> <p>Example of a not found page</p> </body></html>

Add the command to htaccess:

Comment Code

1. Navigate to the root folder www.2. Open the .htaccess file3. Add the command shown on the right

last line.4. Save the file

# This file provides security to the server limiting access to the localhost only.# Comment to deactivate.

Order Deny,AllowDeny from allAllow from 127.0.0.1

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

AddHandler cgi-script .pl .cgiOptions +ExecCGI

# 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

Page 5: Htaccess - Uniform Server Wiki System

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

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

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

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

Categories: UniCenter | Support | Troubleshooting | Application | Development

Page 6: Htaccess - Uniform Server Wiki System

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

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

Htaccess: Prevent Directory ListingFrom Uniform Server Wiki System

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

Contents[hide]

1 htaccess commands2 Personalise index page listings

.htaccess - Apache directory-level configuration file

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

If you have read the [Htaccess: Site error documents | site error documents page]] you will have created a folder named error. Type thefollowing 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.varindex.htm, index.php3, index.phpindex.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 commandsThere 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

IndexIgnore *.gif *.jpg You can be selective and state the file types you do not want listed. Again the wildcard matches all files, in thisexample all gif and jpg image files are targeted and will not be displayed. While all others will be displayed.

Top

Personalise index page listingsIf you are not going to prevent folder listings consider personalising the page displayed.

Page 7: Htaccess - Uniform Server Wiki System

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

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

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 thehtaccess file as follows:

File name Comment

HEADER.html

This is just a text file containing something like this:

<h1>Power of htaccess</h1>

Note: You can insert any regular HTML tag. They are not complete HTML pages just snippets that are included.

README.html

Again this is a text file that uses any regular HTML tag for example:

<h1>More Power of htaccess</h1>'''<p>Why! The name README and not FOOTER Ahave no idea</p>

Note: You can insert any regular HTML tag. They are not complete HTML pages just snippets that are included.

Top

RicRetrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Prevent_Directory_Listing"

Categories: UniCenter | Support | Troubleshooting | Application | Development

Page 8: Htaccess - Uniform Server Wiki System

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

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

Htaccess: RedirectFrom Uniform Server Wiki System

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

Contents[hide]

1 Redirect format1.1 File Redirect format1.2 Folder Redirect

2 Directory example 13 Directory example 24 Practical Example

4.1 Errors

.htaccess - Apache directory-level configuration file

Redirect

The htaccess command Redirect is very powerful you can redirect files or complete folders. Why would you want to do this a typical examplewould 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 oreven new page name.

Redirect formatThe 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. RedirectThis is the Apache command followed by a space

2. /olddirectory/oldfile.htmlThis is the path to the old file you want redirectedIt is stated in a relative file ...temAgain followed by a space

3. http://yoursite.com/newdirectory/newfile.htmlThis 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 namesredirecting a folder is similar to the above for example:

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

1. Redirect

Page 9: Htaccess - Uniform Server Wiki System

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

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

This is the Apache command followed by a space2. /olddirectory

This is the path to the old directory you want redirectedIt is stated in a relative file formatAgain followed by a space

3. http://yoursite.com/newdirectoryThis is the full (URL) path of the location you want the request redirected to

Top

Directory example 1Using our errors folder make a copy of it and rename it to moved.

Comment Code

1. Navigate to the root folder www.2. Open the .htaccess file3. Add the command shown on the right

last line.4. Save the file

# This file provides security to the server limiting linkingto the localhost only.# Comment to deactivate.

Order Deny,AllowDeny from allAllow from 127.0.0.1

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

AddHandler cgi-script .pl .cgiOptions +ExecCGI

# 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.bat2. In the browser address bar type http:/localhost/old3. You will be redirected to the folder http://localhost/moved/

Top

Directory example 2Open 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.bat2. In the browser address bar type http:/localhost/old/not_found.html3. 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

Page 10: Htaccess - Uniform Server Wiki System

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

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

I was faced with this very problem UniCenter has been around for nearly two years and started to become unmanageable hence the move overto the Wiki. Many web sites link to various pages on UniCenter to avoid frustration of not finding an article; users are redirected to theappropriate 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_JoomlaRedirect /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_hMailServerRedirect /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:_HomeRedirect /new_users/new_users_install_1.html http://wiki.uniformserver.com/index.php/New_Users:_Quick_Install_GuideRedirect /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 redirectedpage.

Errors

Any typos in the .htaccess file will cause Apache to spit out the following error message:

Internal Server ErrorThe 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

RicRetrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Redirect"

Categories: UniCenter | Support | Troubleshooting | Application | Development

Page 11: Htaccess - Uniform Server Wiki System

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

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

Htaccess: Preventing hot linkingFrom Uniform Server Wiki System

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

Contents[hide]

1 Serve a broken image2 Serve an alternative image3 General note

.htaccess - Apache directory-level configuration file

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 anynon 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 imageThis code produces a broken image to be displayed when its hot linked. Make sure to replace "mydomain.com" with your own.

Options +FollowSymLinksRewriteEngine onRewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www1. ) ?mydomain. com/ . *$ [NC]RewriteRule \ . ( gif | jpg )$ - [ F]

Or

Options +FollowSymLinksRewriteEngine onRewriteCond %{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 imageI 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 +FollowSymLinksRewriteEngine onRewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]RewriteRule \.(gif|jpg)$ http://www.mydomain.com/nasty.gif [R,L]

General note

Page 12: Htaccess - Uniform Server Wiki System

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

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

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

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

Categories: UniCenter | Support | Troubleshooting | Application | Development