Easing Some .htaccess Pain
Just wanted to post a quick tip that resulted from a Tweet from Jonathan Longnecker of FortySeven Media. He asked:
Anyone got Dreamhost to do 301 redirects in tandem with removing #eecms index.php? Weird query string stuff happening.
Well, having a bit of experience working with Dreamhost, I responded. This problem isn’t specific to ExpressionEngine, it actually will apply to any website or application where you are using .htaccess to rewrite index.php out of the URLs. Dreamhost and many shared hosts like it is are set up in such a way that .htaccess is always a bit more painful than usual, using the query string URI protocol. (Not that .htaccess isn’t painful to begin with.)
A typical .htaccess file on one of these hosts would look like this:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [QSA]
That works out fine until you want to add some 301 redirects into the mix. If you add a standard redirect like:
Redirect 301 /oldpath /newpath
You’ll find that instead of redirecting to www.domain.com/newpath, it ends up going to www.domain.com/newpath/?/oldpath, which isn’t good at all.
The key to fixing this is making sure that your index.php removal rule excludes your 301 redirects.
Just add this line after the RewriteEngine On line:
# put all your redirects inside the parentheses, separated by |s RewriteCond $1 !^(old|old1|old2) [NC]
Hopefully that’s helpful to someone out there. Thanks to Jonathan for asking a question that I could actually answer.
-
http://twitter.com/GDmac GDmac
-
http://confluxgroup.com Jeremy Gimbel



