# +---------------------------------------------------------------------- # | Apache配置示例 - H5路由支持 # +---------------------------------------------------------------------- # | 用于支持uni-app H5端的History路由模式 # | 将此文件重命名为 .htaccess 并放置在H5构建目录的根目录 # +---------------------------------------------------------------------- # 开启重写引擎 RewriteEngine On # 设置重写基础路径(根目录) RewriteBase / # 不重写index.html RewriteRule ^index\.html$ - [L] # 不重写静态资源文件 RewriteRule ^(static|css|js|fonts|images)/ - [L] # PC页面不重写 RewriteRule ^static/html/pc\.html$ - [L] # 如果请求的文件存在,直接返回 RewriteCond %{REQUEST_FILENAME} !-f # 如果请求的目录存在,直接返回 RewriteCond %{REQUEST_FILENAME} !-d # 所有其他请求重写到index.html RewriteRule . /index.html [L] # +---------------------------------------------------------------------- # | Gzip压缩配置 # +---------------------------------------------------------------------- # 压缩HTML、CSS、JavaScript、Text、XML和字体 AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # 移除压缩文件的浏览器bug BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent # +---------------------------------------------------------------------- # | 浏览器缓存配置 # +---------------------------------------------------------------------- ExpiresActive On # 图片缓存1年 ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType image/svg+xml "access plus 1 year" ExpiresByType image/x-icon "access plus 1 year" # CSS和JavaScript缓存1个月 ExpiresByType text/css "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType text/javascript "access plus 1 month" # 字体缓存1年 ExpiresByType application/x-font-ttf "access plus 1 year" ExpiresByType application/x-font-woff "access plus 1 year" ExpiresByType application/x-font-woff2 "access plus 1 year" ExpiresByType font/ttf "access plus 1 year" ExpiresByType font/woff "access plus 1 year" ExpiresByType font/woff2 "access plus 1 year" # HTML不缓存 ExpiresByType text/html "access plus 0 seconds" # +---------------------------------------------------------------------- # | 安全头配置 # +---------------------------------------------------------------------- # X-Frame-Options防止点击劫持 Header always set X-Frame-Options "SAMEORIGIN" # X-Content-Type-Options防止MIME类型嗅探 Header always set X-Content-Type-Options "nosniff" # X-XSS-Protection防止XSS攻击 Header always set X-XSS-Protection "1; mode=block" # Referrer-Policy控制Referer信息 Header always set Referrer-Policy "no-referrer-when-downgrade" # +---------------------------------------------------------------------- # | 字符编码 # +---------------------------------------------------------------------- AddDefaultCharset utf-8 AddCharset utf-8 .html .css .js .xml .json .rss # +---------------------------------------------------------------------- # | 禁止访问特定文件 # +---------------------------------------------------------------------- # 禁止访问.htaccess文件 order allow,deny deny from all # 禁止访问备份文件 order allow,deny deny from all # +---------------------------------------------------------------------- # | 使用说明 # +---------------------------------------------------------------------- # # 1. 将此文件重命名为 .htaccess # mv .htaccess.example .htaccess # # 2. 复制到H5构建目录的根目录 # cp .htaccess /var/www/html/h5/ # # 3. 确保Apache启用了以下模块: # - mod_rewrite (URL重写) # - mod_deflate (Gzip压缩) # - mod_expires (缓存控制) # - mod_headers (HTTP头控制) # # 4. 启用模块命令(Ubuntu/Debian): # sudo a2enmod rewrite # sudo a2enmod deflate # sudo a2enmod expires # sudo a2enmod headers # # 5. 修改Apache配置允许.htaccess覆盖: # 编辑 /etc/apache2/sites-available/000-default.conf # 在块中添加: # # AllowOverride All # # # 6. 重启Apache: # sudo systemctl restart apache2 # # +----------------------------------------------------------------------