服务器.htaccess 配置文件详解

2012年09月02日 网站建设 暂无评论 阅读 3,538 次




RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]

RewriteRule \.(gif|jpg)$ – [F]


RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]

RewriteRule \.(gif|jpg)$ http://www.mydomain.com/替代图片文件名 [R,L]


.htaccess is a very useful way to accomplish things that HTML and CSS can’t, however it is very rarely allowed on free-servers. You should check with your system administrator to make sure that .htaccess is allowed/enabled before trying out these tutorials.

1.error pages: customize your 404 error pages.

We all know what a 404 “Not Found” error page looks like. This tutorial will show you how to make those error pages anything you desire.

First you’ll need to create your error pages. Just make some normal HTML pages, one for each error type. The error types and their definitions are:

400: Bad Request. The server doesn’t understand the request.

401: Authentication Failed. The password was not accepted.

403: Access Forbidden. Access to the area is forbidden.

404: File Not Found. The requested file could not be found.

500: Internal Server Error. Usually the result of a misconfigured script.

Once you’ve made your error pages, upload them to a directory called error or something of the sort.

Now the important part. Create a file called htaccess.txt in Notepad. Add these lines to the file:

ErrorDocument 400 http://iiwnet.com/error/400.html

ErrorDocument 401 http://iiwnet.com/error/401.html

ErrorDocument 403 http://iiwnet.com/error/403.html

ErrorDocument 404 http://iiwnet.com/error/404.html

ErrorDocument 500 http://iiwnet.com/error/500.html

Change iiwnet.com to the location of your website. Save the file and upload it to your top-most web directory. Once it’s there, rename it with your FTP program to .htaccess – complete with the dot starting it out, and with no .txt extension this time.

If the .htaccess file is in your top-most web directory, then all errors that occur in that directory and all its subdirectories will be referred to the appropriate error page.

That’s it! Try it out by going to a page on your website that you know doesn’t exist.

2.private directories: password protect your private folders.

Ever wanted a password protected directory? Click here for an example, use kali as your username and green as the password. With .htaccess and .htpasswd you can create as many users and passwords as you need.

To have one of your own, download this cgi script: password.cgi.

To use it, unzip the script and upload it to your cgi-bin.

CHMOD it 755 and run it from your web-browser.

Instructions will be detailed in the script.

3.anti-leech: stop people from linking to your image files.

If you’ve ever gone through your site logs and found someone hotlinking to your image files, stealing your bandwidth, then you need the following lines in your .htaccess file:

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://iiwnet.com/.*$ [NC]

RewriteCond %{HTTP_REFERER} !^http://www.iiwnet.com/.*$ [NC]

RewriteRule .*\.(gif|GIF|jpg|JPG|bmp|BMP)$ – [F]

Change “iiwnet.com” to your own URL, and upload the .htaccess file to the directory containing your images. Once it is in place, only people coming from “iiwnet.com” will be able to view the images; everyone else will only see a broken image placeholder. If you have an image you’d like to display instead of the placeholder, use the version below.

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://iiwnet.com/ [NC]

RewriteCond %{HTTP_REFERER} !^http://www.iiwnet.com/ [NC]

RewriteCond %{REQUEST_URI} !^/theif.gif [NC]

RewriteRule \.(gif|GIF|jpg|JPG)$ [R]

“theif.gif” is the image you want to load whenever someone attempts to hotlink an image from your server. You only need to add the RewriteCond %{REQUEST_URI} !^/theif.gif [NC] line if the image you want to load is in a directory that the .htaccess file effects. Otherwise you can remove the line, and it will still function.


icon_question icon_razz icon_sad icon_evil icon_exclaim icon_smile icon_redface icon_biggrin icon_surprised icon_eek icon_confused icon_cool icon_lol icon_mad icon_liuhan icon_rolleyes icon_wink icon_idea icon_arrow icon_neutral icon_cry icon_mrgreen icon_qinqin icon_bizui