I\'m trying to add a simple 301 rule to the .htaccess file of a codeigniter site.
redirect 301 /newsletter http://sub.domain.com/newsletters/may2010
Heh, this is Apache pulling a sneaky order-of-processing trick on you. As it turns out, the fact that you put your Rewrite
command at the top of the file doesn't mean that it's what's actually executed first. What happens instead is that mod_rewrite
is run first, followed by mod_alias
(which is responsible for handling your Rewrite
).
This results in the following transformation, per mod_rewrite
:
newsletter --> index.php?/newsletter
mod_rewrite
happily sets the query string to ?/newsletter
, but because you don't have the PT
flag specified, does not passthrough the rewritten URL index.php
to mod_alias
. Therefore, mod_alias
still sees the /newsleter
path, redirects it to http://sub.domain.com/newsletters/may2010
, and appends the (now changed) query string ?/newsletter
to the end to seal the deal. Fun stuff, right?
Anyway, a quick-fix for this scenario would be to ignore requests for just newsletter
:
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !newsletter$
RewriteRule ^(.*)$ index.php?/$1 [L]