[TOC] ## .htaccess中配置的场景 ### 自定义错误页面 在`.htaccess` 中 ``` ErrorDocument 404 /404.html ErrorDocument 500 /500.htm ``` ### 限制迅雷 通过特征分析,限制迅雷访问 ``` RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC,OR] --如果是迅雷在HTTP_USER_AGNET中就有2.0.50727 RewrtieCond %{HTTP_USER_AGENT} ^BlackWido ^BlackWido [NC,OR] RewriteRule . /demo.txt ``` ### 防盗链 不显示图片 ``` RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.demo.com/.*$ [NC] RewriteRule \.(gif|jpg|png)$ –[F,NC] ``` 显示禁止的图片 ``` RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.demo.com/.*$ [NC] RewriteRule \.(gif|jpg|png)$ localhost/demo/2.jpg [R] ``` ### 带post 的参数转发到其他域名 > [参数](https://blog.csdn.net/mengzuchao/article/details/80593789) 可在`.htaccess` 中配置 `RewriteRule ^(Api/User/get_dept_id)$ http://192.168.0.123:82/$1 [L,R=301,P]` 并且加载 `http.conf`的两个模块 ``` LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so ``` ### 重定向指定url 开启 `mod_proxy.so`与`mod_proxy_http.so` ``` ProxyPass /imapp/depts http://127.0.0.1:8081/imapp/depts ProxyPass /api/dept/op_record http://127.0.0.1:8081/api/dept/op_record ``` ### URL所有重定向 ``` RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] ``` //可添加判断,是否开启重写模块 ``` <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule> ``` ### 永久 /临时 重定向 301-永久重定向 302-临时重定向 ``` Redirect permanent index.php home.php #永久重定向 Redirect temp index.php home.php #临时重定向 ``` ### 重定向移动设备 ``` RewriteEngine On RewriteCond %{REQUEST_URI} !^/m/.*$ RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC] #------------- The line below excludes the iPad RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$ #------------- RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW RewriteRule ^(.*)$ /m/ [L,R=302] ``` ### 强制浏览器下载指定的文件类型 你可以强制浏览器下载某些类型的文件,而不是读取并打开这些文件,例如MP3、XLS。 ``` <Files *.xls> ForceType application/octet-stream Header set Content-Disposition attachment </Files> <Files *.eps> ForceType application/octet-stream Header set Content-Disposition attachment </Files> ``` ### 使用.htaccess缓存 给网站提速 调试时谷歌浏览器需要关闭缓存开关 ``` # 1 YEAR <FilesMatch "\.(ico|pdf|flv)$"> Header set Cache-Control "max-age=29030400, public" </FilesMatch> # 1 WEEK <FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> # 2 DAYS <FilesMatch "\.(xml|txt|css|js)$"> Header set Cache-Control "max-age=172800, proxy-revalidate" </FilesMatch> # 1 MIN <FilesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=60, private, proxy-revalidate" </FilesMatch> ``` ### 404页面跳转,跳转到404.php,根据url记录不存在页面的路径 ``` <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_METHOD} POST RewriteCond %{REQUEST_URI} .wp-comments-post\.php* RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR] RewriteCond %{HTTP_USER_AGENT} ^$ RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L] </IfModule> ``` ### 只允许 get,post 请求 apache 2.4版本 中 ``` <LimitExcept GET POST> Require all denied </LimitExcept> ```