A bin for my favorite articles
Posts tagged .htaccess
10 htaccess Hacks Every SEO Should Know
Jul 25th
There’s a lot that you can do with an htaccess file, and of course, things can get pretty advanced in a hurry. Here, we’re going to keep things pretty simple. These are the 10 basic htaccess hacks that every webmaster should know.
1. Force Caching with htaccess
Use: The following htaccess code won’t help the initial pageload, but it will significantly help subsequent pageloads by sending 304 status when requested elements haven’t been modified.
FileETag MTime Size
ExpiresActive on
ExpiresDefault “access plus x seconds”
I generally set the cache for one day (86400 seconds). You can also set different expirations for each file type by breaking each file type up into separate ExpiresByType lines such as:
ExpiresByType image/gif “access plus x seconds”
ExpiresByType text/css “access plus x seconds”
Simple!
2. Set a Custom 404 Page with htaccess
Use: I think this one is self explantatory. Just change ‘/notfound.html’ to match the path to your custom 404 page.
ErrorDocument 404 /notfound.html
3. Implement a 301 Redirect with htaccess
Use: If you have permanently changed the URL structure on your site (via either optimization change or CMS migration), you will want to implement 301 redirects from the old URL to the new URL.
The syntax for a basic 301 redirect is:
Redirect 301 relative/path/to/oldurl/ http://www.domain.com/newurl/
Explanation:
The first URL should be a relative path to the old URL and the second one should be an absolute path to the new URL.
4. Only allow specific IP addresses in certain directories
This is especially useful for admin directories. I generally set my home IP and work IP as the only allowable IPs who can even attempt a login. Unlike other .htaccess hacks, this one doesn’t work from the root folder. You will need to create a new .htaccess file, put the following code in it, and upload it to your admin directory.
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName “Example Access Control”
AuthType Basic
order deny,allow
deny from all
allow from xx.xx.xx.xx
To allow a second IP, just add another ‘allow from’ line.
5. Prevent Image Hot Linking with htaccess
Removed. Here is a MUCH better way to go about this thanks to Maurizio Petrone
6. Enable gzip with htaccess
Gzip is a means of compressing the files on your server so they will load faster. To enable gzip, just
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
7. Remove ‘category’ from a URL with htaccess
Many content management sytems use the ‘category’ URL segment. For instance:
http://makeitrank.com/category/category-name
Well, that’s fine, and it’s necessary to make the CMS work the way it’s supposed to, but it doesn’t need to be visible to do its job. Just drop the following code into your htaccess file to get rid of it.
RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]
8. Define any page as the home page with htaccess
You can set any page as the homepage by adding the following to the htaccess file in your root directory.
DirectoryIndex myfile
9. Disable Directory Browsing
You want to keep people out of any directories that might reveal security weaknesses — for instance, plugin directories.
You can block vistors from browsing the directories by adding the following line to the htaccess file in the directory you’d like to block:
Options All -Indexes
10. Protect your htaccess file
Lastly, you want to protect your htaccess file. Hopefully your host has protected it for you, but you can’t be too safe with these things. The following hack will prevent anybody from accessing your htaccess:
<Files .htaccess>
order allow,deny
deny from all
</Files>
Password Protecting Your Pages with .htaccess
Mar 2nd
Tutorial by Matt Doyle. Level: Intermediate. Published on 3 October 2005 in Apache.
Learn how to use Apache’s .htaccess files to protect pages on your site with a username and password.
Introduction
You may have visited a web page that pops up a dialog box similar to this one:
If you don’t know the username and password to enter, then you can’t access the page or site – it’s “password protected”. It’s sometimes handy to be able to password protect your pages like this – for example:
-
You’re building a new site, but you only want yourself (and maybe a select few) to be able to view the work-in-progress.
-
You have an area of your site that you never want the general public to have access to – for example, your web stats or private pages.
-
You have some paid (subscription) content on your site that only subscribers should be able to access.
Apache lets you password protect individual files, folders, or your entire site fairly easily. Read on to find out how it’s done.
How it works
To add password protection to your pages, you need to do the following two things:
- Create a text file on your server that will store your username and password.
- Create a special file called
.htaccessin the folder you want to protect.
That’s it! Now let’s take a look at how to do each step.
Creating the password file
The first step is to create a simple text file that will store your username and password, separated by a colon (:). The small catch is that the password must be encrypted. Luckily, there are many free web-based utilities that will encrypt the password for you. Try one of these:
- 4WebHelp’s online .htpasswd encryption tool
- Alterlinks .htaccess password generator
- htmlite’s htpasswd encryption page
Simply enter your desired username and password in one of these pages and submit the form. You’ll get back a string similar to the following:
fred:p29cmnwl4a0et
Now, open up your favourite text editor (e.g. Notepad or TextEdit), then copy and paste the username/password string into the editor. Save the file and call it .htpasswd.
Next, upload this file to your website. Make sure you place it outside the Web root of your site if possible, as you don’t want just anyone to be able to view the file! For example, place it above your public_html or htdocs folder. (Having said this, Apache is often set up by default to block web-based access to files beginning with .ht. Better safe than sorry though!)
If you can’t place your .htpasswd file outside your Web root, name it something that’s not easily guessable – for example, .htxuymwp - so that people won’t be able to find it easily. (In addition, it helps to start the filename with .ht; as mentioned earlier, Apache usually blocks access to files starting with .ht.)
Alternative: Creating the password file using htpasswd
If you have SSH access to your web server (or you’re running Apache on a local machine), you can encrypt your password and add it to your password file in one go by using thehtpasswd utility that comes with Apache. Simply SSH to your server or open up a terminal window on your local machine, cd to the folder where you want to create your password file, and type:
htpasswd -c .htpasswd fred
(where fred is the username you want to use). You’ll be prompted to enter and retype your password, then the .htpasswd file will be created for you.
Creating the .htaccess file
Now that you have created and uploaded your password file, you need to tell Apache to use it to protect your page(s) or site. This is what your .htaccess file will do.
Open your text editor again, create a new file, and save it as .htaccess.
Protecting a folder
To password protect a folder on your site, you need to put the following code in your.htaccess file:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Folder"
Require valid-user
/full/path/to/.htpasswd should be the full path to the .htpasswd file that you uploaded earlier. The full path is the path to the file from the Web server’s volume root – for example,/home/username/.htpasswd or C:\wwwroot\username\.htpasswd. (If you’re not sure of the full path to your site or home directory, ask your Web hosting company for this info.)
The above .htaccess file will password protect all files in the folder that it is placed in, and all sub-folders under that folder too. So if you wanted to password protect your entire site, you would place the .htaccess file in your Web root folder.
Protecting a file
To password protect just a single file in a folder, use the following .htaccess file:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Page"
<Files "mypage.html">
Require valid-user
</Files>
This will password protect just the mypage.html file in the folder where you put the.htaccess file.
Uploading the .htaccess file
Once you’ve created your .htaccess file, upload it to your website, placing it in the folder (or folder containing the file) that you want to protect.
Testing it out
Now use your Web browser to visit the folder or file that you’ve protected. You should see a password dialog like the one shown at the start of this tutorial. Type in the username and (unencrypted) password that you chose earlier, and you should be given access to your folder or file!
(By the way: with this type of password protection, you continue to have access to the password protected stuff until you restart your browser.)
Problems?
If you can’t access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn’t work, check the path to your .htpasswdfile on the server – make sure the path specified in the AuthUserFile directive is correct. Also make sure that both the .htpasswd and .htaccess files are readable by the Web server user (chmod 644 should do the trick for UNIX/Linux/FreeBSD servers).
If the password protection isn’t working (i.e. you can still access your stuff without needing to enter a username/password), check that you uploaded your .htaccess file to the right folder. Also check that your web server supports .htaccess password protection (it needs to be an Apache server, and your server admin needs to have enabled the AuthConfigoverride for your site).
Password protecting more stuff
- If you want to password protect other folders (that aren’t under the currently protected folder), simply copy your
.htaccessfile to the new folder to be protected. - To password protect more than one file in the same folder, just create more
<Files></Files>blocks within the same.htaccessfile – for example:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Page"
<Files "mypage.html">
Require valid-user
</Files>
<Files "myotherpage.html">
Require valid-user
</Files>
Adding more usernames and passwords
You’re not restricted to just one username/password. If you want to add other usernames and passwords, simply repeat the “Creating the password file” procedure above, but add each new username/password line to your existing .htpasswd file, e.g.:
fred:p29cmnwl4a0et
linda:vwp45xakfh89
Alternatively, if you’re using htpasswd to create your passwords, as described earlier, then you can add extra users with the command:
htpasswd .htpasswd linda
(where linda is the username you want to add). Make sure you don’t include the -c option when adding additional users, or htpasswd will attempt to create a new password file!
Further info
For full information on Apache’s mod_auth module (the module that does password protection, amongst other things), see the Apache mod_auth documentation.