Vários Snippets htacces para você!

Olá, hoje estou trazendo alguns snippets(pedaços de código) para htacces, neles estão incluídos pedaços de código para cache, gzip, redirecionamentos e etc.

Performance com htacces

# Desabilitar ETAGS
<FilesMatch "\\.(ico|pdf|flv|jpe?g?|png|gif|js|css|swf|txt|mp3|avi|mpe?g?|wmv)$">
 FileETag none


# Compressão de arquivos

 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript
 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4\.0[678] no-gzip
 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Snippets canonicos

# CANONICAL ROBOTS.TXT

 RedirectMatch 301 ^/(.*)/robots\.txt http://seusite.com/robots.txt


# CANONICAL SITEMAP

 RedirectMatch 301 /sitemap\.xml$ http://seusite.com/sitemap-press.xml
 RedirectMatch 301 /sitemap\.xml\.gz$ http://seusite.com/sitemap-press.xml.gz


# MULTIPLE SITEMAPS

 RewriteBase /
 RewriteCond %{REQUEST_URI} !^/sitemap\-(categoria1|categoria2)\.xml(.gz)?$ [NC]
 RewriteRule /sitemap\-(.*)?\.?(.*)?(.*)? http://seusite.com/sitemap-$1.$2$3 [R=301,L]


# CANONICAL XMLRPC

 RedirectMatch 301 /press/(.*)/xmlrpc\.php$ http://seusite.com/xmlrpc.php


# FORCE TRAILING SLASH

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


# ROOT CANONICALIZATION

 RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]
 RewriteRule ^index\.php$ http://seusite.com/ [R=301,L]
 RewriteCond %{HTTP_HOST} ^www\.seusite\.com$ [NC]
 RewriteRule (.*) http://seusite.com/$1 [R=301,L]


# CANONICALIZATION

 # REMOVE INTITIAL INDEX.PHP
 RedirectMatch 301 index.php/(.*) http://seusite.com/press/$1


 # REMOVE ADDITIONAL INDEX.PHP
 RewriteBase /press/
 RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html|php)\ HTTP/
 RewriteRule ^(([^/]+/)*)index\.(html|php)$ http://seusite.com/press/$1 [R=301,L]
 # FORCE TRAILING SLASH
 RewriteCond %{REQUEST_URI} /+[^\.]+$
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]


# REDIRECT PAGE QUERIES

 RewriteCond %{REQUEST_URI} !seusite.com [NC]
 RewriteCond %{REQUEST_URI} !^/$ [NC]
 RewriteCond %{QUERY_STRING} ^p\= [NC]
 RewriteRule (.*) http://seusite.com/? [R=301,L]


 # REDIRECT SUBDIRECTORIES

 RewriteBase /
 RewriteCond %{REQUEST_URI} ^/(dir1|dir2)/?$
 RewriteRule .* http://seusite.com/ [R=301,L]


# CLEAN EXTERNAL LINKS

 RewriteBase /
 RewriteCond %{QUERY_STRING} scamdex [NC]
 RewriteRule .* http://seusite.com/$1? [R=301,L]

Snippets de segurança

# HOTLINK PROTECTION

 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !/hotlink\-(01|02).gif$ [NC]
 RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?seusite\. [NC]
 # RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
 RewriteRule \.(gif|jpe?g?|png)$ http://seusite.com/wordpress/hotlink-02.gif [R,NC,L]


# BLOCK EVIL REQUESTS

 Options +FollowSymLinks
 RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
 RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
 RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
 RewriteRule .* blacklist.php [F,L]


# BLOCK SCUM REFERRERS

 RewriteCond %{HTTP_REFERER} (.*)secondchanceranch(.*) [NC]
 RewriteRule .* - [F,L]


# DENY ACCESS TO NO-REFERRER REQUESTS

 RewriteCond %{REQUEST_METHOD} POST
 RewriteCond %{REQUEST_URI} .wp-comments-post\.
 RewriteCond %{HTTP_REFERER} !.*seusite\. [OR,NC]
 RewriteCond %{HTTP_USER_AGENT} ^$
 RewriteRule .* - [F,L]


# REDIRECT URL (fafich.ufmg.br)

 RewriteCond %{REQUEST_FILENAME} .*
 RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)?ufmg\. [NC]
 RewriteRule .* - [F,L]

Fonte: http://perishablepress.com/code-snippets/

1 Comentários

  1. Gostei muito do post, vou procurar aprender mais sobre htaccess…esse post ajuda os novatos,kkkkkk

Deixe um comentário

Por favor, seja educado. Seu e-mail não será publicado e os campos obrigatórios estão marcados.