Difference between revisions of "Htaccess"

From KOP KB
Jump to: navigation, search
(301 Redirect)
Line 62: Line 62:
 
</Files>
 
</Files>
  
 +
</syntaxhighlight>
 +
 +
== htaccess caching ==
 +
<syntaxhighlight lang="apache">
 +
<IfModule mod_headers.c>
 +
# YEAR
 +
<FilesMatch ".(ico|gif|jpg|jpeg|png|flv|pdf)$">
 +
Header set Cache-Control "max-age=29030400"
 +
</FilesMatch>
 +
# WEEK
 +
<FilesMatch ".(js|css|swf)$">
 +
Header set Cache-Control "max-age=604800"
 +
</FilesMatch>
 +
# 24 HOURS
 +
<FilesMatch ".(html|htm|txt|php)$">
 +
Header set Cache-Control "max-age=86400"
 +
</FilesMatch>
 +
</IfModule>
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 19:16, 3 April 2015

Redirect for SSL

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Another SSL redirect option

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Two domains pointing at a website one has a different starting point.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^$ http://domain.com/filename.html [L,R=301]

301 Redirect

Redirect 301 /oldfile.htm http://example.net/

Note that while the old file is a necessary parameter, it can be set to '/' (without the single quotes) to define all requests to that folder. Such as:

Redirect 301 / http://example.net/

Some people like to use this so it appears its only without www, you could do the reverse and have it for non www go to www

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

Auth for Single Page or Multiple

Protect pages via htaccess or even whole directories.

This will help with creating the htpasswd file http://www.htaccesstools.com/htpasswd-generator/

<Files "mypage.html">
#if you want the whole directory you don't require the Files clause here
# also if you have the entirety of the auth info in the files clause you can do separate files with separate login information
AuthType Basic
AuthName "Protected Page"
AuthUserFile /Full/Path/To/.htpasswd
Require valid-user
</Files>

htaccess caching

<IfModule mod_headers.c>
# YEAR
<FilesMatch ".(ico|gif|jpg|jpeg|png|flv|pdf)$">
 Header set Cache-Control "max-age=29030400"
</FilesMatch>
# WEEK
<FilesMatch ".(js|css|swf)$">
 Header set Cache-Control "max-age=604800"
</FilesMatch>
# 24 HOURS
<FilesMatch ".(html|htm|txt|php)$">
 Header set Cache-Control "max-age=86400"
</FilesMatch>
</IfModule>