Tuesday, October 4, 2016

Redirecting Index Pages with mod_rewrite

I recently got a request from our web team to redirect any index page to its parent.  The problem was that www.utica.edu/about and www.utica.edu/about/index.cfm both loaded the same content and were causing problems with search engines indexing our site.  To solve this, the web team wanted any request for an index page to return a 301 (resource permanently moved) redirect to the parent.  Thus, www.utica.edu/about/index.cfm would get permanently redirected to www.utica.edu/about/.

We're running Apache with mod_rewrite, so the solution was to implement a rewrite rule in the Apache configuration.  The rule to blindly redirect any index page to its parent is actually quite simple:

RewriteCond %{REQUEST_URI} ^(.*)/index.cfm$ [NC]
RewriteRule ^(.*)/index\.cfm$ $1/ [NC,R=301,L]
Note: In our case, the index pages are always named index.cfm.  You could replace this with index.htm, index.html, index.php, or whatever your index page is defined as and it should still work.

The above rule works fine for blindly redirecting any index page, but causes problems if you have a web form which submits to an index page.  To work around this, you can add another condition to make sure the request method is not POST:

RewriteCond %{REQUEST_URI} ^(.*)/index.cfm$ [NC]
RewriteCond %{REQUEST_METHOD} !^post$ [NC]
RewriteRule ^(.*)/index\.cfm$ $1/ [NC,R=301,L]

Likewise, you could make the condition even more narrow by explicitly matching GET requests only, but this might be overkill for most purposes:

RewriteCond %{REQUEST_URI} ^(.*)/index.cfm$ [NC]
RewriteCond %{REQUEST_METHOD} !get$ [NC]
RewriteRule ^(.*)/index\.cfm$ $1/ [NC,R=301,L]

I hope this is helpful if you're in the same situation.