16个简单实用的.htaccess使用技巧

2019-10-15 12:32:36王冬梅

Order deny,allow
Deny from all
Allow from ip
Allow from ip

9. 直接找开文件而不是下载 – 通常,我们打开网上文件的时候总是会出现一个对话框问我们是下载还是直接打开,使用下面的设置就不会出现这个问题了,直接打开。

AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov

10. 修改文件类型 – 下面的示例可以让任何的文件都成为PHP那么被服务器解释。比如:myphp, cgi,phtml等。

ForceType application/x-httpd-php
SetHandler application/x-httpd-php

11. 阻止存取.htaccess 文件

# secure htaccess file
order allow,deny
deny from all

12. 保护服务器上的文件被存取

# prevent access of a certain file order allow,deny
deny from all

13. 阻止目录浏览

# disable directory browsing
Options All -Indexes

14. 设置默认主页

# serve alternate default index page
DirectoryIndex about.html

15. 口令认证 – 你可以创建一个文件用于认证。下面是一个示例:

# to protect a file

AuthType Basic
AuthName “Prompt”
AuthUserFile /home/path/.htpasswd
Require valid-user

# password-protect a directory
resides
AuthType basic
AuthName “This directory is protected”
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

16. 把老的域名转像新的域名

# redirect from old domain to new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]