Aquesta és una revisió antiga del document


.htacess, tutorial y ejemplos

  • mantén el fichero .htaccess tan pequeño como pueda ser
  • comenta y organiza correctamente el fichero .htaccess
  • añade la opción [L] a las páginas finales, para decirle al servidor que no procese más reglas - como en hotlinking
  • el fichero .htaccess tienen herencia en las carpetas, ojo con el fichero en el raiz
  • denegar acceso total a carpeta:
    deny from all
  • denegar acceso total, excepto determinada IP o rango:
    deny from all
    allow from x.x.x.x
    allow from y.y.y.y/x
  • denegar acceso a un archivo:
    <Files fichero.extension>
    order allow,deny
    deny from all
    </Files>
  • carpetas navegables:
    Options +Indexes +MultiViews +FollowSymlinks
  • más opciones para las carpetas navegables:
    IndexOptions FancyIndexing
  • prevenir el listado de carpetas:
    IndexIgnore *
  • activar compresión de datos inherente a PHP:
    php_value zlib.output_compression 16386
  • usando la directiva Files sobre el fichero a ocultar y:
    order allow,deny
    deny from all
    satisfy all
  • ErrorDocument 404 /errores/noencontrado.html
  • que la página tenga más de 512 bytes para evitar la «funcionalidad» de Internet Explorer de sustituir por una página propia las que pesan menos de eso
  • RewriteEngine on
    RewriteCond %{HTTP_REFERER} ejemplo\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} otroejemplo\.com
    RewriteRule .* - [F]
  • Si el visitante no es de mi sitio -example.com- y pide un fichero de esas extensiones, redireccionalo a un arhivo .gif :
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?example\.com[NC]
    RewriteRule .*\.(zip|mp3|avi|wmv|mpg|mpeg)$ http://www.example.com/images/nohotlink.gif [R,NC,L]
  • ojo con enlaces que si que quieres permitir - como un RSS - a alguno de tus ficheros
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule .* - [F]

/via:http://deteresa.com/redireccion-301/ hay que tener el mod_rewrite activo

Options +FollowSymlinks
RewriteEngine on
RedirectMatch permanent ^/(.*) http://<url>/$1

redireccionar de una página a otra u otro dominio:

Redirect 301 /vieja-pagina http://www.dominio.com/nueva-pagina

redireccionar solo la página principal del dominio:

RedirectMatch 301 ^/$ http://www.dominio.com/nueva-pagina

redireccionar un directorio:

Redirect 301 /viejo-directorio/ http://www.dominio.com/nuevo-directorio/

redirección de un subdirectorio + sus páginas:

RedirectMatch 301 ^/viejo-directorio/(.*)$ http://www.dominio.com/nueva-pagina

redireccionar dominio

Redirect 301 / http://www.dominio.com
RedirectMatch 301 ^(.*)$ http://www.dominio.com

Redireccionar 301 (de técnicos de Colt)

RewriteEngine on
rewriterule ^(.*)$ http://www.volvocars.com/es/sales-services/sales/volvoselekt/Pages/ [r=301,nc]

redireccionar de www a no-www:

<IfModule mod_rewrite.c>
   RewriteCond %{HTTP_HOST} ^example.com [NC]
   RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>

redireccionar de no-www a www

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^www.example.com [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>

Más información: http://deteresa.com/redireccion-301

eliminar las www:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.example\.com[nc]
RewriteRule ^(.*)$ http://example.com/$1 [r=301,nc]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

cancelar sitio completo «por obras»:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*secret-cookie.*$ [NC]
RewriteRule .* maintenance-page.html [NC,L]

/vía: http://www.besthostratings.com/articles/force-ssl-htaccess.html utilizar la redirección 301 para evitar penalización en Google:

Options +FollowSymlinks
RewriteEngine on
rewriterule ^(.*)$ http://www.nuevapaginaweb.com/$1 [r=301,nc]
<?php
$web_url ="http://www.nuevapaginaweb.com";
header("HTTP/1.1 301 Moved Permanently");
header("Location: $web_url");
exit(0);
?>

redirección http a https: activar el módulo rewrite en apache

$ sudo a2enmod rewrite
# Copia el contenido siguiente en tu archivo .htaccess y colócalo en la raíz de tu sitio web.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.dominio.com/$1 [R,L]

# Para forzar la conexión segura en un folder en especifico.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.dominio.com/folder/$1 [R,L]
Para este segundo caso el archivo .htaccess debe estar ubicado en el folder donde necesitas activar HTTPS.

de www. a <nada> para evitar penalizaciones de doble indexación:

RewriteEngine On
RewriteCond% {} HTTP_HOST! ^ su-site.com $ [NC]
RewriteRule ^(.*)$ http://your-site.com/ $ 1 [L, R = 301]

redireccionar una petición de la raiz de la página web a una carpeta:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^entregaelbalon\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.entregaelbalon\.com$
RewriteCond %{REQUEST_URI} !^/copadelrey/
RewriteRule (.*) /copadelrey/ [L]

en este caso, las peticiones a www.entregaelbalon.com irán automaticamente a www.entregaelbalon.com/copadelrey (si se intenta con redirect da bucle)

es necesario renombrar los ficheros a la nueva extensión

# Make PHP code look like unknown types
AddType application/x-httpd-php .cool

permite contenido dinámico dentro de páginas estáticas que se intepretan por el servidor http://httpd.apache.org/docs/2.2/howto/ssi.html para hacerlo sobre páginas con extensión .shtml (por no mezclar con las .html, aunque se puede y puede que sea más sencillo si tienes algo en marcha):

Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
AddHandler server-parsed .shtml  # ?

debe estar habilitado el auth_basic.load en /etc/apache2/mods-enabled

AuthName "Directorio privado"
AuthType Basic
AuthUserFile .htpasswd
require valid-user
  • es necesario crear el fichero .htpasswd con los usuarios/contraseñas
  • el fichero .htpasswd he de tener ruta absoluta o relativa al ServerRoot
  • se puede proteger un archivo, poniendo el código dentro de una directiva Files

debe estar habilitado el auth_digest.load en /etc/apache2/mods-enabled

 

niega cualquier conexión desde cualquier sitio

deny from all

no muestra listado de ficheros

Options -Indexes
Indexignore *
Directoryindex index.php index2.php
  • web/apache/htaccess/start.1535615028.txt.gz
  • Darrera modificació: 30/08/2018 00:43
  • per mate