您现在的位置是:网站首页 > 脚本编程>

常用的.htaccess 提示和技巧

2013-11-124424人围观
简介 Apache可以通过.htaccess实现任何的功能,下面介绍一下.htaccess常用的功能1. 定制目录的 Index 文件1DirectoryIndex index.html index.php index.htm你可以使用上面的配置来更改目录的默认页面,例如你将这个脚本放在 foo 目录,则用户请求 /foo/ 时候就会访问 /foo/index.html。2. 自定义错误页1.ErrorDocument 404 errors/404.html当用户访问页面报错时

Apache可以通过.htaccess实现任何的功能,下面介绍一下.htaccess常用的功能。

1. 定制目录的 Index 文件

DirectoryIndex index.html index.php index.htm

你可以使用上面的配置来更改目录的默认页面,例如你将这个脚本放在 foo 目录,则用户请求 /foo/ 时候就会访问 /foo/index.html。

2. 自定义错误页

ErrorDocument 404 errors/404.html

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

ErrorDocument 404 /psych/cgi-bin/error/error?404

  3. 控制访问文件和目录的级别

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

# no one gets in here!
deny from all

上述脚本是拒绝所有的访问,你也可以根据IP段来拒绝:

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

一般这些方法是通过防火墙来处理,但在一个生产环境中的服务器来说,这样的调整非常方便。

有时候你只是想禁止某个ip访问:

# someone else giving the ruskies a bad name..
order allow,deny
deny from 83.222.23.219
allow from all
4. 修改环境变量

环境变量包含了服务器端 CGI 的一些扩展信息,可使用 SetEnv 和 UnSetEnv 进行设置以及取消设置.

SetEnv SITE_WEBMASTER "Jack Sprat"
SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com
UnSetEnv REMOTE_ADDR
5. 301 重定向

如果你希望某个页面跳转到新的页面:

Redirect 301 /old/file.html http://qi522.com/a/1.html

下面可以实现对整个路径的重定向

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

  6. 通过 .htaccess 实现缓存策略

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

# year
<FilesMatch".(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch".(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch".(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
7. 使用 GZIP 对输出进行压缩

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

<IfModulemod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

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

<Location>
    SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI 
        .(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI 
        .(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>

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

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

更多关于压缩的内容请阅读: Compressing PHP, CSS, JavaScript(JS).

8. URL 重写

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

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

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

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

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

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

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

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

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

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


打赏本站,你说多少就多少

本文地址:https://www.qi522.com/view/42.html

来     源:千奇博客

精彩评论

微信关注

Copyright © 2013-2019 千奇博客 保留所有权利 辽ICP备13008238号