WordPress网站定制开发专家
WordPress企业主题特惠

赞一个 0

我们为您整理11个在网站建站过程中非常有用的 .htaccess 代码,使用时把对应代码复制粘贴到网站根目录下的  .htaccess 文件中即可,请记住修改前一定要备份该文件。

1、强制 URL 以斜杠结尾

在 URL 结束的位置如果加上斜杠(/),对 SEO 有很大帮助,这段代码将帮助你实现该功能:

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_URI} /+[^\.]+$
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>

猛击这里查看详细情况:http://perishablepress.com/code-snippets/

2、禁止图片盗链

盗链这种恶劣的行为通常大量地浪费服务器贷款。这段代码很实用,可以重定向盗链图片到一个指定的默认图(在第六行定义):

RewriteEngine On
#Replace ?mysite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mangguo\.org/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

猛击这里查看详细情况:.htaccess 设置图片防盗链

3、重定向移动客户端访问

如果站点没有使用设备自适应的设计方案,这段代码可以帮助你将网站重定向到移动版本:

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]

猛击这里查看详细情况:http://snipplr.com/view.php?codeview&id=55114

4、强制下载某一格式的文件

处于某些原因,需要使得某些文件访问时直接提示用户下载保存,例如 MP3、Office 文档等。这段代码可以轻松搞定:

<Files *.xls>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>
<Files *.eps>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>

猛击这里查看详细情况:强制下载文件

5、Firefox 跨域字体嵌入

当嵌入字体时,Firefox 不允许从外部网站调用。使用 .htaccess 代码可以突破这个限制:

<FilesMatch "\.(ttf|otf|eot|woff)$">
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "http://www.mangguo.org"
</IfModule>
</FilesMatch>

猛击这里查看详细情况:http://snipplr.com/view/53703

6、使用 .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>

猛击这里查看详细情况:http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html

7、阻止 WordPress 博客中的垃圾评论

在为你博客的垃圾评论头痛?当然 Akismet 很有用,但一旦 Akismet 挂了,.htaccess 就能凑效了:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*mangguo.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
</IfModule>

猛击这里查看详细情况:使用 .htaccess 减少 WordPress 垃圾评论

8、重定向 RSS 源到单一格式

以前 RSS,Atom 或者 Rdf 经常被使用。而现在貌似 RSS 是最为流行的。这段代码允许你把所有的订阅源格式重定向到某个单一源,WordPress 同样适用。

<IfModule mod_alias.c>
 RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/
 RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/
</IfModule>

猛击这里查看详细情况:http://www.wprecipes.com/redirect-feeds-to-a-single-format

9、配置网站的 HTML5 视频媒体类型

HTML5 技术为网站开发带来了很多令人振奋的选择,在众多炫酷的特性里,一个重要的地方是使用了 HTML5 视频代替了 Flash 技术。现在可以直接在网页中播放 HTML5 视频,但在此之前,你需要配置服务器来让它更好地支持:

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

猛击这里查看详细情况:http://snipplr.com/view.php?codeview&id=53437

10、记录 PHP 错误

和通常在页面中显示 PHP 运行错误的方式不同,这段代码将会把日志写入 .log 文件,避免直接在页面中暴露错误:

# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log

猛击这里查看详细情况:http://css-tricks.com/snippets/htaccess/php-error-logging/

11、在 JavaScript 文件中运行 PHP 代码

编写 JavaScript 代码时,如果能在 JS 文件中使用 PHP 那就牛逼哄哄了,比如直接从数据库获取数据,以下是一个在 JS 文件中使用 PHP 的示例:

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js

<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

转载自 芒果小站

上一篇:

下一篇:

在线评论

在线客服
在线客服关闭
WPYOU官方微信

扫码关注官方微信