.htaccess
htaccess and client sided routing
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# forward everything to index.html, except existing files and folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
</IfModule>
URI redirect for RDF resolve
RewriteEngine On
RewriteBase /id/
RewriteCond %{REQUEST_URI} ^/id/(.+)$
RewriteRule ^(.+)$ https://data.netwerkdigitaalerfgoed.nl/hetutrechtsarchief/Test-Amerongen/browser?resource=https://hetutrechtsarchief.nl%{REQUEST_URI} [R=302,L]
404 handler
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+\.jpg)$ HANDLER.php [L,QSA]
</IfModule>
redirect folder to other domain in root htaccess
RewriteRule ^FOLDER.*$ https://DOMAIN [R=301,L]
add www to your domain name when there isn't a subdomain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
redirect all traffic to new domain
RewriteEngine On
RewriteRule ^(.*)$ https://newdomain.nl/ [R=301]
.htaccess: Invalid command 'ExpiresActive', perhaps misspelled or defined by a module not included in the server configuration
ln -s /etc/apache2/mods-available/expires.load /etc/apache2/mods-enabled/
# or
sudo a2enmod expires
Directory index
DirectoryIndex first.html
CORS
<Files "*.json">
Header add Access-Control-Allow-Origin "*"
</Files>
redirect to index.php in paged/ folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /paged/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /paged/index.php [L]
</IfModule>
then to extract /:page use:
preg_match('/^\/(.*)/',$_SERVER["REDIRECT_URL"], $matches, PREG_OFFSET_CAPTURE);
$page = explode("/",$matches[1][0])[1];
list files directoryIndex
Options +Indexes
force download
<FilesMatch "\.d3sketch$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
(pre-compressed) gzip
AddEncoding gzip .gz
Access-Control-Allow-Origin * with X-Requested-With
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
Basic Auth
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /FOLDER/.htpasswd
<limit GET POST>
require valid-user
</limit>
redirect
RewriteEngine On
##############################
# redirect /doku.php?id=xxx #
###############################
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^doku\.php$ http://wiki.companje.nl/%1? [R=302,L]
##############################
##############################
# redirect if file not found #
##############################
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://wiki\.companje\.nl/$1 [L,R=301]
#############################
Examples in Dutch
http://andrescholten.nl/voorbeelden-veel-gebruikte-htaccess-regels/
internal server error while using 'Header add'
enable Apache's headers module: sudo a2enmod headers && sudo service apache2 restart
Access-Control-Allow-Origin
http://enable-cors.org/server_apache.html or put this header in your php script:
header('Access-Control-Allow-Origin: *');
show php errors
# don't supress php errors
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
disable cache
Header append Cache-Control "no-cache"
mod_rewrite cheat sheet
download as attachment
<FilesMatch "\.(mov|mp4)$">
Header set Content-Disposition attachment
</FilesMatch>
create SHA encrypted password for htpasswd
htpasswd -s FOLDER/.htpasswd USERNAME
redirect subdomain to other url
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule (.*) YOUR_URL [R=301,L]
with subfolder in query: (place this before the code above)
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com
RewriteCond %{REQUEST_URI} ^/lijst
Rewriterule ^(.*)$ http://otherdomain.com [L]
htaccess ignored?
set 'AllowOverride' to 'All' in httpd.conf within
examples in dutch
redirect to other domain with keeping folder
RewriteCond %{HTTP_HOST} =doodle3d.com
RewriteRule (.*) http://www.doodle3d.com/$1
redirect subdomain
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^kc\.doodle3d\.nl$ [NC]
RewriteRule (.*) http://kunstcentraal.doodle3d.nl [R]
serve .csv as text mime type
AddType text/plain .csv
Enable directory listing
Options +Indexes
Disable directory listing
Options -Indexes
Redirect if URL equals HTTP_HOST
RewriteRule ^$ /nl [R=301,L]
Unprotect a subdir of a htaccess password-protected directory
create a .htaccess in the subfolder with this in it:
Satisfy any
change max upload in PHP/ upload_max_filesize
edit your .htaccess:
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200