-
-
Save benjaminbixby/1375657 to your computer and use it in GitHub Desktop.
ExpressionEngine index.php removal with Google Analytics Compatibility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################################################################# | |
| # | |
| # - 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" | |
| # | |
| ################################################################################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment