Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following 2 links, I'm not great with .htaccess rules yet.

Old URL:
New URL:

I want all the traffic coming from the old URL to the new url (including the parameters charity_id & project_id).

I'm trying to learn .htaccess rules, but finding the tutorials online to be kinda vague.

I'd really like a simple explanation on the .htaccess rules.

(Give a man a fish, feed him for a day, teach a man to fish, feed him for a lifetime).

The correct answer will be the answer with a simple and useful explanation (along with the rules if possible!).

share|improve this question
up vote 2 down vote accepted

To learn to set up .htaccess rules you at least need some little regexp skills and be confident to use mod_rewrite Apache module, since it comes handy to build rewriting rules thanks to its flexibility.

If so, your code can be like

RewriteEngine On
RewriteCond %{QUERY_STRING} ^charity_id=([0-9]+)&project_id=([0-9]+)$
RewriteRule donate/donate.php [L,R=301]

At (for 2.4 Apache version, choose a different one if applicable) is available an useful reference, so you have to start from it, even for better understand the code above.

You can think at RewriteRule as the brick for rewriting, so every rule will use it at least once. You will going to add before RewriteCond when you need to check environment vars or to reference query string values, like in our piece of code.

To place a backreference into substitution string coming from previous RewriteCond you will use from %1 to %9 values; if you need backreference from first part of RewriteRule itself, you will use $1 to $9.

Mind the question mark sign at the end of substitution url, since it's the way to skip off the query string; if missing, you will have the ?charity_id=885&project_id=18111 at the end of new url.

share|improve this answer
Accepted this as the correct answer, because the explanation helped me loads!! Thanks so much! – Anil Oct 20 '12 at 17:08

In another post, sysadmin1138 has written a very good introduction into mod rewrite.

You can find the post here.

/EDIT: Example:



RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
share|improve this answer
Dude this is exactly what I was looking for. – Anil Oct 19 '12 at 16:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.