An .htaccess file controls how Apache interacts with your site. When an .htaccess file is placed in your domain’s directory, the .htaccess file is detected and executed by Apache.
An .htaccess file is commonly used for the following:
- Denying specific IPs to your site
- Password protecting your site
- Redirecting specific pages
- Rewriting URLs
- Custom error pages
This article details how to create and view an .htaccess file in Windows and Mac OS X.
Using an .htaccess file on your website is unlikely to cause any problems. However, it is possible for errors to occur if the rules entered into the file are incorrect which may cause your site to suddenly display a 500 error. If something like this occurs, you can comment out the rules/lines you have just added by placing a ‘#’ in front of each line. Repeat this until you find the line causing the error.
Creating an .htaccess file in Windows
- Open a new document in Notepad.
- Save it with the name .htaccess and make sure ‘All files’ is selected in the ‘Save as type’ drop-down menu. Doing this ensures it doesn't save it with the .txt extension.
- When you upload an .htaccess file to your account using FTP, make sure that the data transfer mode is set to ASCII. Never use BINARY since it is a text file.
- In your FTP client, change the permissions on the file to
- This makes the file readable by your web server, but at the same time disables browsers from reading it.
- The public should never be able to read your .htaccess file.
A few things to note when creating this file in Windows:
- Make sure that your text editor has word wrap disabled. If you don't, your text editor might add characters to the file that will cause problems with the web server which results in a non-functional .htaccess file.
- Make sure that all of your commands in the .htaccess file are on a separate line.
- When you use an .htaccess file on your web server, the file affects the current directory and any of its subdirectories. So if you place an .htaccess file in the root directory of your website, it affects every directory on your website.
Viewing .htaccess files in Mac OS X
Because Mac OS X is based on BSD UNIX, it follows the UNIX convention of hiding all files whose names begin with '.' from view. While listing, creating, and editing such files is possible from Terminal (e.g., ls -a), such files and folders don't usually show up in the Mac GUI, including the Finder and Open dialog boxes.
You can set the Finder to show invisible files in Terminal:
- Click ‘Go’ > ‘Utilities’ and open the Terminal app.
- Enter the following:
defaults write com.apple.finder AppleShowAllFiles YES ; killall Finder
- If you’re using Yosemite, run the following command instead:
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder
This is simple to reverse: just change the argument from "YES" to "NO" or “true” to “false”. The following article gives you further information regarding this:
Alternatively, you can edit and upload .htaccess files via UNIX software in Terminal, or use a different name such as "htaccess", and then rename the file to ".htaccess" after it has been uploaded to the site.
Finding .htaccess files in your FTP client
Your FTP client may suddenly refuse to display the file once you rename it to .htaccess. View the show hidden files article for instructions on how to view these files in different clients.