Baza znanja: Web Hosting
.htaccess podešavanja - primeri
Postavio modifikovano 02 December 2013 11:07

.htaccess (hypertext access) je konfiguracioni fajl na web serverima koji koriste Apache web server softver. .htaccess je puno ime fajla i to nije ekstenzija.

Preusmeravanje jedne stranice

RewriteEngine on
Redirect 301 /stara_stranica.html http://www.domen.com/nova_stranica.html

Preusmeravanje svih stranica na index stranu novog domena

RewriteEngine on
Redirect 301 / http://www.novi-domen.com/

Preusmeravanje svih stranica na relevantne strane novog domena

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.stari-domen.com$ [NC]
RewriteRule ^(.*)$ http://www.novi-domen.com/$1 [R=301,L]

Promena ekstenzije fajlova (html u php)

RewriteEngine on
RedirectMatch 301 (.*)\.html$ http://www.domen.com$1.php

Redirekcija sa www na verziju bez www

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domen\.com
RewriteRule (.*) http://domen.com/$1 [R=301,L]

Redirekcija bez www na www verziju

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domen.com [NC]
RewriteRule ^(.*)$ http://www.domen.com/$1 [L,R=301]

Skrivanje ekstenzije fajlova

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

Keširanje u browseru

Ovo govori browseru da drži određene fajlove duže u svom kešu (na primer slike, koje se retko menjaju).

# BEGIN Expire headers  
 
    ExpiresActive On  
    ExpiresDefault "access plus 5 seconds"  
    ExpiresByType image/x-icon "access plus 2592000 seconds"  
    ExpiresByType image/jpeg "access plus 2592000 seconds"  
    ExpiresByType image/png "access plus 2592000 seconds"  
    ExpiresByType image/gif "access plus 2592000 seconds"  
    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"  
    ExpiresByType text/css "access plus 604800 seconds"  
    ExpiresByType text/javascript "access plus 216000 seconds"  
    ExpiresByType application/javascript "access plus 216000 seconds"  
    ExpiresByType application/x-javascript "access plus 216000 seconds"  
    ExpiresByType text/html "access plus 600 seconds"  
    ExpiresByType application/xhtml+xml "access plus 600 seconds"  
 
# END Expire headers

Alternativno možete da koristite mod_headers

# BEGIN Caching
<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>
# END Caching

Omogucavanje parsiranja .html kao .php
 

U .htacces file-u treba upisati sledece linije:

 

<filesmatch "\.html$"="">
ForceType application/x-httpd-php5
</FilesMatch>

Radi provere da li sve radi, u root-u kreirati PHP file koji se recimo zove phptest.html i upisati sledece:

 

<?PHP
echo "PHP je podesen"
?>

Nakon sto se taj file kreira, testirati ga u browser-u. Treba da otvari "PHP je podesen"

(10 glas(ova))
Koristan
Nije koristan