几个有用的Apache下.htaccess设置

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-24 06:21   11   0

Apache Web 服务器可以通过 .htaccess 文件来操作各种信息,这是一个目录级配置文件的默认名称可用来重写服务器的全局配置。该文件的目的就是为了允许单独目录的访问控制配置,例如密码和内容访问。对于拥有空间的朋友都喜欢用它来优化网站配置。

本文提供几个.htaccess的配置供参考。

本文提供的代码基本可以直接复制到.htaccess,部分需要修改路径和文件名。

1、自定义错误页

  1. ErrorDocument 404 errors/404.html

当用户访问页面报错时,例如页面找不到你希望显示自定义的错误页面,你可以通过这种方法来实现。或者是动态的页面:

  1. ErrorDocument 404 errors/404.html

2、301 重定向

如果要让某个页面跳转到新的页面:

  1. Redirect 301 /old/file.html http://yourdomain.com/new/file.html

或者要对整个路径的重定向:

  1. RedirectMatch 301 /blog(.*) http://yourdomain.com/$1

3、强制要求使用 HTTPS 访问

通过以下脚本可以强制整个网站必须使用 https 方式访问:

  1. RewriteEngine On
  2. RewriteCond %{HTTPS} !on
  3. RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

4、阻止列出目录和文件

使用下面代码可以防止列表目录和目录里的文件:

  1. Options -Indexes

或者

  1. IndexIgnore *

5、添加 MIME-Types

MIME-types 依赖于文件的扩展名,未能被识别的文件扩展名会当成文本数据传输,或者是下载文件

  1. AddType application/x-endnote-connection enz
  2. AddType application/x-endnote-filter enf
  3. AddType application/x-spss-savefile sav

6、指定上传文件的大小限制,适用于PHP

  1. php_value upload_max_filesize 20M
  2. php_value post_max_size 20M
  3. php_value max_execution_time 200
  4. php_value max_input_time 200

7、禁止指定后缀的脚本执行

  1. Options -ExecCGI
  2. AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

8、设置字符集和语言头

  1. AddDefaultCharset UTF-8
  2. DefaultLanguage en-US

9、设置自定义的响应 Headers

  1. Header set P3P "policyref=\"http://www.askapache.com/w3c/p3p.xml\""
  2. Header set X-Pingback "http://www.askapache.com/xmlrpc.php"
  3. Header set Content-Language "en-US"
  4. Header set Vary "Accept-Encoding"

10、设置服务器时区(GMT)

  1. SetEnv TZ America/Indianapolis

11、定制目录的 Index 文件

  1. DirectoryIndex index.html index.php index.htm

12、控制访问文件和目录的级别

.htaccess 经常用来限制和拒绝访问某个文件和目录,例如我们有一个includes文件夹,这里存放一些脚本,我们不希望用户直接访问这个文件夹,那么通过下面的脚本可以实现:

  1. # no one gets in here!
  2. deny from all

也可以根据IP段来拒绝:

  1. # no nasty crackers in here!
  2. order deny,allow
  3. deny from all
  4. allow from 192.168.0.0/24
  5. # this would do the same thing..
  6. #allow from 192.168.0

禁止某个ip访问:

  1. # someone else giving the ruskies a bad name..
  2. order allow,deny
  3. deny from 83.222.23.219
  4. allow from all

13、通过 .htaccess 实现缓存策略

通过设置在浏览器上缓存静态文件可以提升网站的性能:

  1. # year
  2. <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
  3. Header set Cache-Control "public"
  4. Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
  5. Header unset Last-Modified
  6. </FilesMatch>
  7. #2 hours
  8. <FilesMatch "\.(html|htm|xml|txt|xsl)$">
  9. Header set Cache-Control "max-age=7200, must-revalidate"
  10. </FilesMatch>
  11. <FilesMatch "\.(js|css)$">
  12. SetOutputFilter DEFLATE
  13. Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
  14. </FilesMatch>

14、使用 GZIP 对输出进行压缩

在 .htaccess 中添加下面的代码可以将所有的 css、js 和 html 使用 GZIP 算法压缩:

  1. <IfModule mod_gzip.c>
  2. mod_gzip_on Yes
  3. mod_gzip_dechunk Yes
  4. mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  5. mod_gzip_item_include handler ^cgi-script$
  6. mod_gzip_item_include mime ^text/.*
  7. mod_gzip_item_include mime ^application/x-javascript.*
  8. mod_gzip_item_exclude mime ^image/.*
  9. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  10. </IfModule>

使用上面代码的前提是启用 mod_gzip 模块,你可以使用下面脚本来判断 Web 服务器是否提供 mod_deflate 支持:

  1. <Location>
  2. SetOutputFilter DEFLATE
  3. SetEnvIfNoCase Request_URI \
  4. \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  5. SetEnvIfNoCase Request_URI \
  6. \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
  7. </Location>

如果 Web 服务器不支持 mod_deflate ,那么可使用下面方法:

  1. <FilesMatch "\.(txt|html|htm|php)">
  2. php_value output_handler ob_gzhandler
  3. </FilesMatch>

15、URL 重写
例如要将 product.php?id=12 重写为 product-12.html

  1. RewriteEngine on
  2. RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

将 product.php?id=12 重写为 product/ipod-nano/12.html

  1. RewriteEngine on
  2. RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

重定向没有 www 到有 www 的 URL 地址:

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} ^viralpatel\.net$
  3. RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]

重写 yoursite.com/user.php?username=xyz 到 yoursite.com/xyz

  1. RewriteEngine On
  2. RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
  3. RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

重定向某个域名到一个 public_html 里新的子文件夹:

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
  3. RewriteCond %{HTTP_HOST} ^www\.test\.com$
  4. RewriteCond %{REQUEST_URI} !^/new/
  5. RewriteRule (.*) /new/$1

16、防盗链
如果不想别人网站引用你站内的图片、css 等静态文件,也就是传说中的防盗链,可以使用如下脚本:

  1. RewriteCond %{HTTP_REFERER} !^$
  2. RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
  3. RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC]
  4. RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [F,NS,L]

17、强制 “File Save As” 提示

  1. AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

18、缓存策略
通过设置在浏览器上缓存静态文件可以提升网站的性能:

  1. # year
  2. <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
  3. Header set Cache-Control "public"
  4. Header set Expires "Thu, 15 Apr 2012 23:00:00 GMT"
  5. Header unset Last-Modified
  6. </FilesMatch>
  7. #2 hours
  8. <FilesMatch "\.(html|htm|xml|txt|xsl)$">
  9. Header set Cache-Control "max-age=7200, must-revalidate"
  10. </FilesMatch>
  11. <FilesMatch "\.(js|css)$">
  12. SetOutputFilter DEFLATE
  13. Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
  14. </FilesMatch>

19、设置自定义的响应 Headers

  1. Header set P3P "policyref=\"http://www.askapache.com/w3c/p3p.xml\""
  2. Header set X-Pingback "http://www.askapache.com/xmlrpc.php"
  3. Header set Content-Language "en-US"
  4. Header set Vary "Accept-Encoding"
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP