0

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!).

Anil
  • 262
  • 2
  • 4
  • 15

2 Answers2

3

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.

alessio
  • 136
  • 1
  • 5
2

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?
High Ball
  • 478
  • 4
  • 11