Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

Old URL: http://www.mywebsite.org.uk/donate/donate.php?charity_id=885&project_id=18111
New URL: http://new.mywebsite.org.uk/donation/to/885/18111

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

2 Answers 2

up vote 1 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 http://new.mywebsite.org.uk/donation/to/%1/%2? [L,R=301]

At http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html (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! –  JustAnil 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:

old: http://example.com/path?var=val 
new: http://example.com/path/var/val

.htaccess

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

Your Answer

 
discard

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.