Removing index.php from ExpressionEngine URLs
I was recently interrupted and informed that our /alumnievents shortcut had broken on Delaware Valley College's website. Absolutely sure it was a mod_rewrite issue, I dove into Coda and started troubleshooting.
Recently launching an Alumni landing page on the site, I was sure that I had broken a rewrite rule with the introduction of the new alumni landing page template. I am using the "include" list method to remove ExpressionEngine's index.php from our site URLs, so I was sure something had to be up with the way EE tells you to do this.
From the EE website:
RewriteEngine on
RewriteCond $1 ^(weblog|member|search|Forum_Name|TemplateGroup_4_Name|TemplateGroup_5_Name|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]With this method you want to include all of the names of your EE Template Groups on the second line. Replace “TemplateGroup_#_Name” with your template groups and “Forum_Name” with the name you’re using for forums (e.g. forums, community, etc).
This is a great way to hide index.php in EE URLs, but it can easily be broken if you have similarly named website shortcuts and EE templates. For example, on the delval.edu website, I am using the above "include" list method to point delval.edu/alumni to the alumni landing page template with the following rule:
RewriteCond $1 ^(alumni) [NC]
RewriteRule ^(.*)$ cms/index.php/landing-pages/$1 [L]
What this actually means in regular expression terms is "match any root level URL that contains 'alumni'". Unfortunately, this also matches /alumniandanythingelseafter, breaking my /alumnievents shortcut.
I'm not sure if EE realizes the forums got this off by just one extra character, but I do hope they find this post and fix the documentation. Here's how to fix it:
RewriteCond $1 ^(alumni$) [NC]
RewriteRule ^(.*)$ cms/index.php/landing-pages/$1 [L]
That's it. Just adding a dollar sign to the end of the word to match will limit mod_rewrite to that word, and only that word. This basically says "match this word but nothing after the last letter".
There is an excellent write up about the dollar sign, and it's sister, the carrot (^) on the Luna Metrics blog.
Shoot me an email if you can think of a way to improve this, or you have mod_rewrite tales of woe to share.
