Câteva coduri utile pentru .htaccess

11/08/2017, Written by 0 comment

Vă prezentăm mai jos câteva coduri .htaccess pe care le considerăm cele mai uzuale.

  1. Redirect site fără www. la www.site.ro (cu https:// inclus)
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^numesite.ro [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.numesite.ro [NC]
    RewriteRule ^(.*)$ https://www.numesite.ro/$1 [L,R=301,NC]
  2. Detectare iPad și redirectare către varianta de iPad a site-ului
    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
    RewriteRule ^(.*)$ http://ipad.domeniu.ro [R=301]

  3. Custom Error Pages
    ErrorDocument 400 /400.html
    ErrorDocument 401 /401.html
    ErrorDocument 403 /403.html
    ErrorDocument 404 /404.html
    ErrorDocument 405 /405.html
    ErrorDocument 408 /408.html
    ErrorDocument 414 /414.html
    ErrorDocument 500 /500.html
    ErrorDocument 502 /502.html
    ErrorDocument 504 /504.html
  4. Descărcare fișier (nu deschidere în browser)
    AddType application/octet-stream .csv
    AddType application/octet-stream .xls
    AddType application/octet-stream .doc
    AddType application/octet-stream .avi
    AddType application/octet-stream .mpg
    AddType application/octet-stream .mov
    AddType application/octet-stream .pdf
  5. Restricționare acces pe bază de IP
    Order deny,allow
    Deny from All
    Allow from xxx.xxx.xxx.xxx
    Allow from yyy.yyy.yyy.yyy
  6. Activare compresie Gzip
    # BEGIN GZIP
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
    </ifmodule>
    # END GZIP
  7. Redirectare pagină/director
    Redirect 301 /pagina-veche.php htttps:/www.domeniu.ro/pagina-noua/
  8. Setare timp de expirare pentru elemente din site
    # 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"
    </IfModule>
    # END EXPIRES