Skip to content

Instantly share code, notes, and snippets.

@julkue
Last active July 28, 2021 15:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save julkue/494e426608af870dec11 to your computer and use it in GitHub Desktop.
Save julkue/494e426608af870dec11 to your computer and use it in GitHub Desktop.
Default .htaccess with configuration redirects to app.php
#/*!***************************************************
#* The MIT License (MIT)
#*
#* Copyright (c) 2014-2017, Julian Motz
#*
#* Permission is hereby granted, free of charge, to any person obtaining a copy
#* of this software and associated documentation files (the "Software"), to deal
#* in the Software without restriction, including without limitation the rights
#* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#* copies of the Software, and to permit persons to whom the Software is
#* furnished to do so, subject to the following conditions:
#*
#* The above copyright notice and this permission notice shall be included in
#* all copies or substantial portions of the Software.
#*
#* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#* THE SOFTWARE.
#*
#* Contributors:
#* Julian Motz - initial API and implementation
#* and initial documentation
#*****************************************************/
#--------------------------------
#-------------Global-------------
#--------------------------------
ServerSignature Off
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php
# All pages in UTF-8
AddDefaultCharset UTF-8
# Set standard Timezone (will change dynamically)
php_value date.timezone "Europe/London"
# Deactivate MultiViews
# MultiViews would redirect to hello.php if you
# request /hello/
# Deactivate also directory browse
Options -Indexes +FollowSymLinks -MultiViews
# Define error-pages
ErrorDocument 401 /error/?m=401
ErrorDocument 403 /error/?m=403
ErrorDocument 500 /error/?m=500
ErrorDocument 502 /error/?m=502
ErrorDocument 504 /error/?m=504
# Force File download, do not display/open in browser
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .tar.gz
AddType application/octet-stream .7zip
AddType application/octet-stream .mov
AddType application/octet-stream .wmv
AddType application/octet-stream .mp4
# Set max upload filesize
php_value upload_max_filesize 100M
php_value post_max_size 100M
# Add security-layer for .htaccess-files
<files .htaccess="">
order allow,deny
deny from all
</files>
#--------------------------------
#------------/Global-------------
#--------------------------------
#--------------------------------
#------------Caching-------------
#--------------------------------
# gzip compresss
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 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 432000 seconds"
ExpiresByType application/x-javascript "access plus 432000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
# Cache-Control Headers
<IfModule mod_headers.c>
# Imagetoolbar deaktivieren (IE)
Header set imagetoolbar "no"
<FilesMatch "\\.(ico|jpe?g|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=432000, private"
</FilesMatch>
<FilesMatch "\\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
</IfModule>
# Turn ETags Off
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
# Remove Last-Modified Header
<IfModule mod_headers.c>
Header unset Last-Modified
</IfModule>
#--------------------------------
#-----------/Caching-------------
#--------------------------------
#--------------------------------
#------------Redirect------------
#--------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Set variable to the current installation route
# so all redirects can perform with this
# if the .htaccess is installed in a sub-directory
# all redirects should also work!
# The variable will contain the current subdirectory
# of the htaccess
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
#--------------------------------
#--------Common Redirects--------
#--------------------------------
# Redirect to domain with www but allow subdomains
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Add slash if there is no at the end
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Remove double slashes in all URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(([^/\ ]+/)*)/+([^\ ]*)
RewriteRule ^ /%1%3 [L,R=301]
#--------------------------------
#------Simluate Directories------
#--------------------------------
# Redirect to URI without front controller to prevent duplicate content
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
#--------------------------------
#-----------/Redirect------------
#--------------------------------
@nightmora
Copy link

How to redirect to domain with NON www ???

@julkue
Copy link
Author

julkue commented Apr 28, 2016

@nightmora Use:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

@nightmora
Copy link

nightmora commented Jun 11, 2016

Sorry my english

this not work
<files .htaccess=""> order allow,deny deny from all </files>

but this yes
<files .htaccess> order allow,deny deny from all </files>

@nightmora
Copy link

Maybe this is better

`# Block access to "hidden" directories or files whose names begin with a period.

This includes directories used by version control systems such as Subversion or Git.

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)." - [F]`

@julkue
Copy link
Author

julkue commented Jun 11, 2016

@nightmora If this works for you, there's no reason why you shouldn't use your solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment