如何配置 .htaccess 文件一直是困扰初学者的难题之一,我在这里特意总结了一下,希望能给初学者启示:
以下有关 .htaccess 的配置兼容因虚拟主机而无权修改 apache 设置的情况。
# Php settings
# Php 配置
# ====================
<IfModule php5_module>
# Adjust memory limit
# 内存限制
# ========================
php_value memory_limit 64M
# Adjust exec time to prevent overtime running
# 最大运行时间
# ================================
php_value max_execution_time 18000
# Make sure the default timezone is set
# Can also be set within application using date_default_timezone_set
# 默认时区
# =====================================
php_value date.timezone "Asia/Shanghai"
# Short tag for clean view scripts which is still not recommend
# Because of the xml issue
# 关闭短标签,防止与 xml 产生兼容性问题
# ==========================
php_value short_open_tag off
# Default charset is utf-8 because of multi languages supportting
# 默认字符集为 utf-8
# ===============================
php_value default_charset "utf-8"
# To report E_ALL|E_STRICT and show all errors
# 默认显示所有错误,即 E_ALL|E_STRICT
# ==============================
php_value error_reporting "8191"
# Disable register globals which means nothing in php6
# 禁止注册超全局变量
# ===========================
php_flag register_globals off
# Disable magic quotes which means nothing in php6
# 禁止自动转义字符串
# ===========================
php_flag magic_quotes_gpc off
# Disable automatic session start but handled by program
# e.g. Zend_Session::start()
# 禁止 session 自动启动,我们交由程序执行,如 Zend_Session::start()
# =============================
php_flag session.auto_start off
# Turn off compatibility with PHP4
# To avoid the problem when dealing with objects
# 关闭向上 (php4) 兼容,以防止处理对象时发生问题
# ======================================
php_flag zend.ze1_compatibility_mode Off
# Gzip output
# Gzip 压缩输出
# =======================================
php_flag zlib.output_compression on
php_value zlib.output_compression_level 9
# Disable user agent verification to not break multiple image upload
# 关闭用户代理认证,使得多图片上传等功能不会中断
# ==================================
php_flag suhosin.session.cryptua off
# Output buffering
# 缓存输出
# =============================
php_value output_buffering 4096
</IfModule>
# Mime type
# Mime 类型
# ====================
<IfModule mime_module>
# Add javascript, php and phtml file type if needed
# 添加 javascript,php 和 phmtl 等文件类型
# =========================================
AddType application/x-javascript .js
AddType application/x-httpd-php .php .phtml
# For 1and1 hosting issue, we use php5 for all
# 对于 1and1 主机,我们统一用 php5
# ==========================
#AddType x-mapp-php5 .php
#AddHandler x-mapp-php5 .php
# For fastcgi mode
# Fastcgi 模式兼容
# =========================================
#Action php5-cgi /cgi-bin/php5-cgi
#AddHandler php5-cgi .php
#cgi.fix_pathinfo = 1
# Default character encoding UTF-8
# 默认字符集为 utf-8
# =====================
AddDefaultCharset UTF-8
# No ETags for performance optimize
# 禁止文件 etag 以优化性能
# ===========
FileETag none
</IfModule>
# Security scan
# 安全扫描
# =======================
<IfModule mod_security.c>
# Disable POST processing to not break multiple image upload
# 禁止对 POST 数据进行安全扫描,使不中断多文件上传等功能
# ===================
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
# Expires by type
# Expires 期限控制
# ======================
<IfModule mod_expires.c>
# Add further expires for resources
# 为所有资源设置过时期限
# ===========================================================
ExpiresActive On
ExpiresDefault "access plus 1 year"
#ExpiresByType text/css "access plus 1 month"
#ExpiresByType image/gif "access plus 1 month"
#ExpiresByType image/jpeg "access plus 1 month"
#ExpiresByType image/png "access plus 1 month"
#ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
# Compress by type
# Compress 根据类型进行压缩过滤
# ======================
<IfModule mod_deflate.c>
# Default filter : deflate
# 默认过滤器 Deflate
# =====================
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
# Netscape 4.x 的一些问题
# =========================================
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
# Netscape 4.06-4.08 有更多问题
# =====================================
BrowserMatch ^Mozilla/4.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# MSIE 的问题
# ===============================================
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Do not compress images
# 不压缩图片
# ===============================================================
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
# The compress level 1(lowest)-9(highest)
# 压缩级别 1(最低) - 9(最高)
# ====================================================
#DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
# Make sure proxies do not deliver the wrong content
# 确保代理不会重复发送错误数据
# ======================
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
# Make HTTPS env vars available for CGI mode
# 使得环境变量在 cgi 模式下有效
# ==================
<IfModule mod_ssl.c>
SSLOptions StdEnvVars
</IfModule>
# Directory and index file
# 文件夹及索引
# ===================
<IfModule dir_module>
# By default allow all access
# 默认所有人都可以访问
# ==========================================
Options -Indexes -MultiViews +FollowSymLinks
Order allow,deny
Allow from all
# Directory default index
# 文件夹默认索引文件
# ===========================================
DirectoryIndex index.php index.html index.htm
</IfModule>
# Url rewrite
# Url 重写
# ======================
<IfModule mod_rewrite.c>
# Rewrite enable
# Url 重写功能开启
# ==============
RewriteEngine On
# Rewrite base path to your site
# Url 重写基本路径
# ===========
RewriteBase /
# Forbidden access to htaccess file
# 禁止访问 .htaccess 文件
# ============================
RewriteRule ^.htaccess$ - [F]
# Url canonicalization for SEO
# Url 强制重定向
# =====================================================
RewriteCond %{HTTP_HOST} ^www.kimbs-local.info [NC]
RewriteRule ^(.*)$ http://kimbs-local.info/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.kimbs.info [NC]
RewriteRule ^(.*)$ http://kimbs.info/$1 [L,R=301]
# Rewrite for index if request is emptyempty or did not mentioned before
# 如果请求不存在,或者未预先定义,则重定向到 index.php
# ================================================================
RewriteCond %{REQUEST_URI} =""
RewriteCond %{REQUEST_URI} !^/(library/dojo)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditor)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditorPlugins)/
RewriteCond %{REQUEST_URI} !^/(library/syntaxhighlighter_2.0.320)/
RewriteRule ^.*$ /public/index.php [NC,L]
# Rewrite for public if request is not the resource under public or did not mentioned before
# 如果请求不是一个可用资源,或者未预先定义,则重定向到 public/ 资源目录
# ================================================================
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteCond %{REQUEST_URI} !^/(library/dojo)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditor)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditorPlugins)/
RewriteCond %{REQUEST_URI} !^/(library/syntaxhighlighter_2.0.320)/
RewriteRule ^(.*)$ /public/$1
# Never rewrite for existing files, directories and links
# 从不为确实存在的文件,文件夹和文件链接进行重定向
# ==============================================
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
</IfModule>