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
In this article, you will find out how to create and view an .htaccess file in Windows and Mac OS x.
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.
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.