Hostwinds 教程
寻找结果为:
目录
标签: htaccess, Optimization
拥有您的网站加载Fast是对大多数主管客户的热情,我们知道这是多么重要。 本指南是帮助您提供审查您的网站及其负载速度,以便您或您的网站开发人员可以看到和测试优化。
我们还将探索许多选项,供您使用和常用方式使用.htaccess文件优化您的网站。 这适用于使用Apache Web服务器共享托管,业务托管,CPanel客户端,VPS,云和专用服务器客户端。
我们将首先使用Google Chrome的内置工具,并使用.htaccess for apache webserver为此指南。
首先要做的是 创建您的.htaccess文件。 在继续本指南之前,很重要的是要备份文件,以确保如果您的网站不加载,或者您有任何问题,则可以恢复此文件。
刷新网页并注意到加载时间,如上面的图像所示。 您还需要注意的是第一个字节的时间,这将是200毫秒的理想选择。 下面的图像显示为120 ms作为示例:
# BEGIN EXPIRES
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days."
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 1 year"
# COMPRESSION
<IfModule mod_deflate.c>
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
# BEGIN GZIP
BEGIN GZIP COMPRESSION
<IfModule mod_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.*
结束压缩
结束GZIP
#CACHE CONTROL
<ifModule mod_headers.c>
<filesMatch ".(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
<filesMatch ".(css)$">
Header set Cache-Control "public"
<filesMatch ".(js)$">
Header set Cache-Control "private"
<filesMatch ".(x?html?|php)$">
Header set Cache-Control "private, must-revalidate."
# FORCE TRAILING SLASH
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# PREVENT HOTLINKING
RewriteEngine On
#Replace ?yourdomainname.com/ with your website url
RewriteCond %{HTTP_REFERER} !^http://(.+.)?yourdomainname.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "hotlinking is not permitted" image url
RewriteRule .*.(jpe?g|gif|bmp|png)$ /hotlinksnotpermitted.jpg [L]
使用实际域名URL和hotlinksnotpermited.jpg替换域名
# REDIRECT MOBILE DEVICES
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]
# STOP SPAM ON WORDPRESS BLOG
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]
使用实际域名URL替换您的域名
# REDIRECT DIFFERENT FEEDS TO A SINGLE FORMAT
<IfModule mod_alias.c>
RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://yourdomainname.com/feed/
RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://yourdomainname.com/comments/feed/
用实际的域名URL替换您的域名
# CONFIGURE YOUR WEBSITE FOR HTML5 VIDEOS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
AddType video/ogg .ogv
AddType video/ogg .ogg
AddType video/mp4 .mp4
AddType video/webm .webm
AddType application/x-shockwave-flash swf
# LOG PHP errors INTO ~/public_html/php_error.log
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag log_errors on
php_value error_log ~/public_html/php_error.log
使用实际域名URL和〜/ public_html / php_error.log替换yourdomainname与日志文件的位置
# RUN PHP INSIDE JAVASCRIPT FILES
AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js
<FilesMatch ".(js|php)$">
SetHandler application/x-httpd-php
#BLOCKING USERS IP
order allow,deny
deny from 123.45.6.7
deny from 0.1.2.3
allow from all
#END BLOCKING USERS IP
替换123.45.6.7和0.1.2.3您要阻止的实际IP地址
#PREVENT DIRECTORY LISTING
IndexIgnore *
#END PREVENT DIRECTORY LISTING
# ERROR DOCUMENT
ErrorDocument code /directory/filename.ext
ErrorDocument 404 /errors/lostandfound.html
用您要使用的错误文档替换/directory/filename.ext您想要使用的404错误页面替换默认错误文档
# REDIRECT NON WWW URL TO WWW URL
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomainname.com
RewriteRule (.*) https://www.yourdomainname.com/$1 [R=301,L]
将您的域名替换为三个位置,/olddirectory/Oldfile.html与旧文件名和newfile.html与新文件名
# SIMPLE REDIRECT
Redirect /olddirectory/oldfile.html https://yourdomainname.com/newfile.html
替换/ olddirectory/oldfile.html和https://yourdomainname.com/newfile.html
#Force SSL without www
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.tld/$1 [R,L]
#Force SSL with www
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.tld/$1 [R,L]
用实际域名替换yourdomain.tld
撰写者 Hostwinds Team / 五月 25, 2018