I use the .htaccess file a lot on hosted servers. On our own servers I prefer to use the httpd.conf as it performs better and is not reevaluated on every request. But if you are on a hosted server the .htaccess is your earliest port of call for handling incoming traffic and can be more efficient than using modules for certain tasks. One common gotcha is how to discard the querystring for a redirect.
I will be posting often about this as managing your .htaccess is a great first step in securing your site and handling common issues to do with missing files and old paths. For the less inclined the URL redirect module is a real ally for many of these jobs.
When Rewriting with the .htaccess a common issue is dealing with the querystring. Sometime you want it and sometimes you don't.
One thing to remember is that if you match for a rewrite the variable created may get appended as a preserved querystring for the redirect.
In the following example we have removed a load of pages in a sub folder and we are getting a lot of 404 errors. We want to redirect all requests to php files in the old folder to the root.
# you find you are getting a lot of 404 errors for old files http://www.example.com/oldsite/oldfile.php?s=5&p=52
A common approach would be to do the following
# match all requests for files in the oldsite folder and redirect them to the root RewriteRule ^oldsite/(.*)$ "http://www.example.com/" [R=301,L]
but we find that when we test this we get redirected OK but to the following:
# the result of the redirect http://www.example.com/?s=5&p=52
i.e. the matched (.*) becomes $1 and QSA may preserve the querystring by default appending it to the redirect URL.
This can be solved by adding a ? to the redirect.
# use the ? to discard the querystring which is otherwise preserved by default RewriteRule ^oldsite/(.*)$ "http://www.example.com/?" [R=301,L]
This will match all calls to oldfile files and the ? on the end of the rewritten URL will discard the Querystring.
info [at] blue-bag.com
Telephone: 0843 2894522
Blue-Bag HQ: The Garage, Manor Farm Chilcompton, Radstock Somerset, BA3 4HP, United Kingdom
Telephone: (+44) 01761 411542
Blue-Bag Brighton: Unit 35 Level 6 North, New England House New England Street, Brighton BN1 4GH United Kingdom