.htaccess
Contents |
What are .htaccess files for?
They are mostly used to define allowed users in a certain directory, or for certain files (given a list of allowed users, or denying everyone), but can also contain RewriteRules, which are used to change the address you load into a different one. They are read from the bottom up, meaning if a page in /dir1/dir2/dir3/ is requested, the .htaccess in dir1 is read first, then dir2, and finally dir3. If options overlap across the files, the closest file to the page being requested will be used.
How do I use them?
You need to create a .htaccess file in the directory you would like to protect or in which you would like the RewriteRules to be activated, and set it according to what you want it to do.
Access restriction
You need to create a password file with the htpasswd command, first doing
htpassword -cb .htpasswd firstuser firstpass
and then all the others with the same line, but without the c (which tells the program to create a new file)
Then in the corresponding .htaccess, you need the following lines (yes, you have to specify an absolute path for the password file...):
AuthType Basic AuthName "Password Required" AuthUserFile /home/b/bob/public_html/.htpasswd require valid-user
You can specify the files you want to protect by wrapping this in <files 'file1' 'file2' ...> and </files>
Rewrite rules
If you want to active this, you first need to put
RewriteEngine on
in your .htaccess file.
Then, you can rules like this one :
RewriteRule ^!(.*)$ http://www.google.com/search?num=50&hl=en&esrch=BetaShortcuts&q=$
This will redirect any query for pages begining with an exclamation mark to a google search, displaying the 50 first results, in English, and using beta shortcuts. It can be quite handy if google is set in a language you don't like or doesn't save your settings, especially if your website has a short url (I'm using it on a website which has a eight-letters long url, that is shorter than google.com !)
You can also redirect to pages internally, but the server is misbehaving a little and will try to access to http://shellium.org/home/b/bob/public_html/new when you try to look for http://shellium.org/~bob/old having a rewrite rule from old to new in the public_html directory. It is, though, still possible.
RewriteRule ^old.html$ $1/~bob/new.php
will get anyone trying to visit your old html page to your new shiny php page. However, this will not be shown in their browser and if the change is permanant, they will still bookmark the old page, which is probably not what you want. You can then specify a permanent redirection by adding [R=301] at the end, like this :
RewriteRule ^old.html$ $1/~bob/new.php [R=301]
You can also specify conditions on the server, since the regexp only match the end of the URL, you wouldn't be able to remove the heading www if you don't like it... (the NC here is for non-case-sensitive) (please note that you still need to specify your user here)
RewriteCond %{http_host} ^www\.shellium\.org [NC]
RewriteRule ^(.*)$ http://shellium.org/~bob/$1 [R=301,NC]