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] .htaccess - Apache directory-level configuration file 1 Whats in a name 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

com/index.com/index.php/Htaccess:_Introduction" Categories: UniCenter | Support | Troubleshooting | Application | Development http://wiki.php/Htaccess:_Introduction 05-11-2010 15:50:49 . Hence htaccess examples found on the Internet or from textbooks should work.uniformserver. Top Ric Retrieved from "http://wiki.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.Htaccess: Introduction . I have included a few to get you started.uniformserver.

Uniform Server Wiki System Página 1 Htaccess: Site error documents From Uniform Server Wiki System . 404 and 500 Top How to create customised error documents Create a new folder in your root folder (www) and name it errors.uniformserver.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 your sites look. This folder contains all the error pages you wish to display.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57 . For example not_found.1 Testing . 403.com/index.Htaccess: Site error documents . Now open the root htaccess file (the one in your root folder www) and add the following line: ErrorDocument 404 /errors/not_found. following are worth considering: 401.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking | Contents [hide] 1 Whats errors produce what 2 How to create customised error documents 3 Example 3. Whats errors produce what The following are a list of the Apache error numbers and what they relate to: Successful Client Requests 201 Created 202 Accepted Client Request Errors 400 Bad Request 401 Authorization Required Server Errors 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 203 Non-Authorative Information 403 Forbidden 204 No Content 205 Reset Content 206 Partial Content Client Request Redirected 300 Multiple Choices 301 Moved Permanently 302 Moved Temporarily 303 See Other 304 Not Modified 305 Use Proxy 404 Not Found 405 Method Not Allowed 406 Not Acceptable (encoding) 407 Proxy Authentication Required 408 Request Timed Out 409 Conflicting Request 410 Gone 411 Content Length Required 412 Precondition Failed 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.html this will be displayed when a page cannot be found on your web site.htaccess .html http://wiki.

Secure Server Access" #AuthType Basic #AuthUserFile /htpasswd/www/.Allow Deny from all Allow from 127.html Top Example The following shows a little more detail first create the error page to be displayed Comment 1.html ErrorDocument 500 /errors/server_error. 4. Note 2: You can use a full URL (as oppose to a virtual path) to your error file for example: http://yoursite.com/errors/forbidden.html file when ever Apache produces the error 404. 2.com/errors/auth_reqired.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 500 http://yoursite.1 1. # Comment to deactivate.html Add the code shown on the right to it.htpasswd #Require valid-user ErrorDocument 404 /errors/not_found.cgi Options +ExecCGI # To unlock your server.Uniform Server Wiki System Página 2 Save the file. Open the . Note 1: The files and folder can be named anything you like. comment the next 4 lines. Run the server using Server_Start. Save the file Code <html> <title>Not found</title> <body> <p>Example of a not found page</p> </body> </html> Add the command to htaccess: Comment Code # This file provides security to the server limiting access to the localhost only.com/errors/server_error.html ErrorDocument 403 /errors/forbidden.0.Htaccess: Site error documents . Order Deny.html ErrorDocument 403 http://yoursite. 2.com/errors/not_found. The command ErrorDocument 404 will forward a user to yoursite/errors/not_found.html Or look like similar to this if when using full URL paths: ErrorDocument 401 http://yoursite.com/errors/not_found. Add the command shown on the right last line.bat http://wiki.html Testing 1. Create a new folder in www named errors.html ErrorDocument 404 /errors/not_found. Save the file # To allow execution of cgi scripts in this directory uncomment next two lines. In this folder create a file named not_found. 4.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57 .htaccess file 3. Password = userver #AuthName "Uniform Server . # Defaults: Username = admin. Navigate to the root folder www.pl . 3.0.uniformserver.com/index. AddHandler cgi-script .html ErrorDocument 404 http://yoursite.

php/Htaccess:_Site_error_documents" Categories: UniCenter | Support | Troubleshooting | Application | Development Página 3 http://wiki.com/index. Apache will generate the error code 404 and redirect to your not_found.uniformserver. 3. In the browser address bar type http:/localhost.com/index.Uniform Server Wiki System 2.php/Htaccess:_Site_error_documents 05-11-2010 15:50:57 .Htaccess: Site error documents .html or some other file that does not exist.fred. Top Ric Retrieved from "http://wiki.html page Experiment with the ErrorDocument command and create a few pages.uniformserver.

var index. Try it on any folder that does not contain any of the following pages index.Uniform Server Wiki System Página 1 Htaccess: Prevent Directory Listing From Uniform Server Wiki System . 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).uniformserver.php/Htaccess:_Prevent_Directory_Listing 05-11-2010 15:51:05 .cgi and you will receive a listing of its contents as would anyone on the internet.php3.gif *. http://wiki.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking | Contents [hide] 1 htaccess commands 2 Personalise index page listings .jpg You can be selective and state the file types you do not want listed.html.htaccess . index.pl.html. Top htaccess commands There is only a single command to learn: Command IndexIgnore * Comment This prevents listing of all the files. Again the wildcard matches all files. index.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 do this.htm. the * is a wildcard that matches all Syste IndexIgnore *.Htaccess: Prevent Directory Listing .shtml. index. in this example all gif and jpg image files are targeted and will not be displayed.com/index.php index. You may or may not find this desirable this page shows you how to prevent this listing. Top Personalise index page listings If you are not going to prevent folder listings consider personalising the page displayed. index. If you have read the [Htaccess: Site error documents | site error documents page]] you will have created a folder named error. While all others will be displayed. index.

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.Htaccess: Prevent Directory Listing .html <h1>More Power of htaccess</h1>''' <p>Why! The name README and not FOOTER A have no idea</p> Note: You can insert any regular HTML tag.com/index. Again this is a text file that uses any regular HTML tag for example: README. They are not complete HTML pages just snippets that are included. They are not complete HTML pages just snippets that are included.php/Htaccess:_Prevent_Directory_Listing" Categories: UniCenter | Support | Troubleshooting | Application | Development http://wiki.com/index.html <h1>Power of htaccess</h1> Note: You can insert any regular HTML tag. Top Ric Retrieved from "http://wiki.uniformserver.Uniform Server Wiki System Página 2 You can personalise the index pages listed by adding a header and footer.uniformserver.php/Htaccess:_Prevent_Directory_Listing 05-11-2010 15:51:05 .

uniformserver. /olddirectory/oldfile..com/newdirectory/newfile.1 Errors Redirect The htaccess command Redirect is very powerful you can redirect files or complete folders.html http://yoursite.html 1.html This is the path to the old file you want redirected It is stated in a relative file .2 Folder Redirect 2 Directory example 1 3 Directory example 2 4 Practical Example 4. 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.1 File Redirect format . http://yoursite.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking | Contents [hide] 1 Redirect format 1.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/newfile.php/Htaccess:_Redirect 05-11-2010 15:51:11 .tem Again followed by a space 3.htaccess . Redirect This is the Apache command followed by a space 2. Redirect http://wiki. 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.com/newdirectory 1.Apache directory-level configuration file 1.Htaccess: Redirect ..com/index.Uniform Server Wiki System Página 1 Htaccess: Redirect From Uniform Server Wiki System .

pl .Htaccess: Redirect .html contained in folder moved add some more text to distinguish it from the original. Comment Code # This file provides security to the server limiting linkingto the localhost only.Uniform Server Wiki System This is the Apache command followed by a space 2. # Comment to deactivate. # Defaults: Username = admin.html 3.html Well you get the idea experiment with files it is similar to the folder example.cgi Options +ExecCGI # To unlock your server. Run the server using Server_Start.com/index.com/newdirectory This is the full (URL) path of the location you want the request redirected to Top Página 2 Directory example 1 Using our errors folder make a copy of it and rename it to moved.Allow Deny from all Allow from 127.uniformserver. Order Deny.php/Htaccess:_Redirect 05-11-2010 15:51:11 . http://yoursite. In the browser address bar type http:/localhost/old/not_found. Navigate to the root folder www.htaccess file 3.1 1. In the browser address bar type http:/localhost/old 3. AddHandler cgi-script . comment the next 4 lines.0. You will be redirected to the file http://localhost/moved/not_found. /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.bat 2. Save the file # To allow execution of cgi scripts in this directory uncomment next two lines. 2. Add the command shown on the right last line.htpasswd #Require valid-user Redirect /old http://localhost/moved Testing 1.Secure Server Access" #AuthType Basic #AuthUserFile /htpasswd/www/. You will be redirected to the folder http://localhost/moved/ Top Directory example 2 Open the file not_found. Password = userver #AuthName "Uniform Server . Run the server using Server_Start. Testing 1. Top Practical Example http://wiki.bat 2. 4.0. Open the .

Below is a small extract from my .com/index.html http://wiki.com/index. makes it easier to isolate and resolve any problems.com/index.php/New_Users:_Problems_Section Tip: I first publish the Wiki pages.php/New_Users:_Quick_Install_ Guide Redirect /new_users/problems_1.com/index. use a browser to view a page and copy the link into .php/Joomla_Portability Redirect /u35_mod_ssl_2/4_hmailserver.php/SSL_Part_2:_Configure_ hMailServer Redirect /new_users/new_users_index.html http://wiki.com/index.com/index.uniformserver.php/Installing_Joomla Redirect /us_35_apps/joomla/joomla_install_2.htaccess and then test.uniformserver.uniformserver. users are redirected to the appropriate page on the Wiki.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.html http://wiki. Top Ric Retrieved from "http://wiki.uniformserver.com/index.php/Htaccess:_Redirect" Categories: UniCenter | Support | Troubleshooting | Application | Development http://wiki.com/index.html http://wiki.uniformserver.html http://wiki.php/SSL_Part_2:_Installing_ hMailServer Redirect /u35_mod_ssl_2/5_hmailserver. Errors Any typos in the .html http://wiki.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.php/New_Users:_Home Redirect /new_users/new_users_install_1.htaccess file you will see I am using page redirection: Redirect /us_35_apps/joomla/joomla_install_1.com/index.html http://wiki.uniformserver.Htaccess: Redirect .php/Htaccess:_Redirect 05-11-2010 15:51:11 .uniformserver. Similar process to obtain the redirected page.uniformserver.htaccess. Hence always make small changes to .uniformserver.

( gif | jpg ) $ . Uniform Server has mod-rewrite enabled. *$ [ NC .[F] Link: I think the original source came from here: webmasterworld. Yep it as complex as it sounds. / ] RewriteRule \ .)?mydomain.)?mydomain.)?(subdomain_mydomain\.Uniform Server Wiki System Página 1 Htaccess: Preventing hot linking From Uniform Server Wiki System .php/Htaccess:_Preventing_hot_linking 05-11-2010 15:51:17 .gif [R.com/index.mydomain. Make sure to replace "mydomain.[ F] Or Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.uniformserver. they are effectively stealing your bandwidth at you expense. It could be images or any non html objects.(gif|jpg)$ http://www.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.*$ [NC] RewriteRule \. Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www1.com" with your own: Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.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. I found this code on the Internet it prevents hot linking to images on your site.(jpe?g|gif|png|bmp)$ .com/nasty.com/ [NC] RewriteRule \. allowing you to rewrite the requested URL on-the-fly based on configuration directives and rules.htaccess .htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking | Contents [hide] 1 Serve a broken image 2 Serve an alternative image 3 General note . ) ?mydomain com .Htaccess: Preventing hot linking . 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.com/.com" with your own.L] General note http://wiki.

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.com/index.0.php/Htaccess:_Preventing_hot_linking" Categories: UniCenter | Support | Troubleshooting | Application | Development http://wiki.Htaccess: Preventing hot linking .php/Htaccess:_Preventing_hot_linking 05-11-2010 15:51:17 .uniformserver.uniformserver.Uniform Server Wiki System 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.0.com/index. This error shows up in the log files as: Página 2 [client 127.