Skip to content

Instantly share code, notes, and snippets.

@ckimrie
Created November 10, 2011 12:18
Show Gist options
  • Save ckimrie/1354725 to your computer and use it in GitHub Desktop.
Save ckimrie/1354725 to your computer and use it in GitHub Desktop.
ExpressionEngine index.php removal with Google Analytics Compatibility
#################################################################################
#
# - ExpressionEngine index.php ModRewrite Removal -
# compatible with Google Analytics Tracking
#
##################################################################################
RewriteEngine on
RewriteBase /
# If the current request is for the homepage, rewrite request to homepage template
# (needed for GA tracking to work) and continue next rewrite rule as normal.
# Replace the site/index with your homepage template_group/template_name
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule (.*) site/index
#################################################################################
# EE SEGMENT TAGS ON HOMEPAGE
# Due to the way in which EE parses URI's and the need for GA tracking ability,
# the above rewrite will mean that on your homepage the segments will be:
#
# {segment_1} = "site"
# {segment_2} = "index"
#
#################################################################################
# The following code removes index.php by checking the request is not:
# 1) An image
# 2) A file
# 3) A directory
#
# If above rules are met, the request is rewritten to index.php whilst removing the
# trailing slash and Query String in the process):
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ /index.php?/$1 [L]
#################################################################################
# [OPTIONAL] LOGGING QUERY STRINGS WITH PHP:
#
# The RewriteRule above strips out the query string so that only JavaScript code
# can access it. To pass the query string through to the server for PHP access, use the
# code below instead of the RewriteRule above:
#
# RewriteRule ^(.*[^/])/?$ /index.php?/$1/&%{QUERY_STRING} [L]
#
# You will need to update your EE config file to allow characters present in GA tracking
# URLs. Find the "permitted_uri_chars" in your config file and update it to the following:
#
# $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\\-&=';
#
# Be careful when doing this, since it effectively makes EE slightly less secure.
#
# NOTE: This rewrite will interfere with your segment conditionals since EE will treat
# the GA tracking code as the last segment.
# eg:
# URL:
# http://website.com/products?utm_source=Twitter&utm_campaign=March
#
# EE Parses this as:
#
# {segment_1} = "products"
# {segment_2} = "?utm_source=Twitter&utm_campaign=March"
#
#################################################################################
@mango-david
Copy link

Thanks for this script, it works great when tracking multiple sites using Google Analytics. However, when I try to use it in conjunction with AJW DataGrab, it seems to be having some issues. AJW DataGrab provides URLs containing get variables and runs the reports when the URL is accessed (URL format is example.com/?ACT=23&id=2).

I thought that the issue would be related to the last comment block in the gist, so I replaced line 45 with line 56, then added in line 61 to my config file. This allowed the AJW DataGrab pages to work correctly, with them returning the expected results. However, when I attempt to access a Google Analytics URL to test the multiple site tracking, I'm presented with a page that says 'Invalid GET Data', with no other markup/styling.

Is this an error that you've encountered before? Perhaps I'm just not using this script in the intended way. Thanks for any help you can offer

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