Skip to content

Instantly share code, notes, and snippets.

@Ottergoose
Last active October 30, 2019 18:51
Show Gist options
  • Save Ottergoose/5534170 to your computer and use it in GitHub Desktop.
Save Ottergoose/5534170 to your computer and use it in GitHub Desktop.
These .htaccess rules will remove index.php from your ExpressionEngine URLs and preserve query strings, which are used by things like Google Analytics. The first file is what we use to dynamically change the rewrite base in dev/staging/production. If you're not interested in doing that, I believe you can remove the "%{ENV:REWRITEBASE}" from the …
# ---------------------------------------------------------------------
# Enable Rewrite Engine
# ---------------------------------------------------------------------
RewriteEngine on
# ----------------------------------------------------------------------
# Dynamic Base Path - Dev - localhost
# ----------------------------------------------------------------------
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:/path/to/project/public_html/]
# ----------------------------------------------------------------------
# Dynamic Base Path - Staging - staging.example.com/~example
# ----------------------------------------------------------------------
RewriteCond %{HTTP_HOST} ^staging.example.com$
RewriteRule . - [E=REWRITEBASE:/~example/]
# ----------------------------------------------------------------------
# Dynamic Base Path - Production - example.com
# ----------------------------------------------------------------------
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule . - [E=REWRITEBASE:/]
# ---------------------------------------------------------------------
# In any request contains "index.php" ...
# ... and is an Apache GET request ....
# ... and isn't in the EE system directory ... (this is still here for legacy sites that do have an system folder in public_html)
# Then ...
# ... redirect to the requested location without "index.php"
# ---------------------------------------------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) %{ENV:REWRITEBASE}$1$2 [R=301,L]
# ---------------------------------------------------------------------
# This rule is needed if query string is given to homepage w/o template name in URI, e.g. example.com instead of example.com/default_template_group_name
# Note: you must may have to change the name of the default template group in this rule to match your setup
#
# If any request request that is not at least 1 char long ...
# ... and contains any sort of query string 1 or more chars in length ...
# ... and doesn't contain a query string of "css=" or "URL=" or "ACT=" ...
# Then ...
# ... process it with /index.php?/[defult template group]/[request]
# ---------------------------------------------------------------------
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond %{QUERY_STRING} !(css|URL|ACT)=
RewriteRule !^(.+)$ %{ENV:REWRITEBASE}index.php?/site/$1 [L]
# ---------------------------------------------------------------------
# This rule is needed to handle query strings in EE
#
# If entire any request ...
# ... doesn't contain an image, css, or js extension, ...
# ... and isn't a file name ...
# ... and isn't a directory name ...
# ... and contains any sort of query string 1 or more chars in length ...
# Then ...
# ... process it with /index.php?/[request]
# ---------------------------------------------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php?/$1 [L]
# ---------------------------------------------------------------------
# If entire request ...
# ... doesn't contain an image, css, or js extension, ...
# ... and isn't a file name ...
# ... and isn't a directory name ...
# Then ...
# ... process it with /index.php/
# ---------------------------------------------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php/$1 [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment