Setting up a redirect using Apache is quite simple, but getting it to work with dynamic URLs can be a lot more difficult. Lets say you want to redirect all traffic from one file to another file in a different location/server. How do you perform this redirect while keeping all of the url variables intact? I have seen some many answers from people that suggest you use a mod rewrite matching method that has all sorts of complicated matching parameters that make it almost impossible to convert that answer into a solution that works for you. This solution is far more simple and will work for most instances without needing to be heavily modified.
In order to setup this redirect you will need to add some code to the .htaccess file of your website. This file can usually be found in the root directory of your website. It is more efficient to use your htaccess file when compared to using PHP. When you use PHP, Apache will need to call it and then PHP does the work. It might be easier to work with PHP, but doing the job with Apache cuts out the work the server has to do. The following piece of code will perform a simple redirect from a file in one server to a file on a different server/subdomain.
RedirectMatch 301 script1.php$ https://newsite.website.com/script1.php$1
It’s as simple as that. By putting the $1 at the end, it is telling apache to match a url that starts “script1.php” and treat everything after this as a variable, when performing the redirect, put all of this content at the end of the new URL. If your files are inside a sub folder you can easily add this onto the first part of the redirect match.
RedirectMatch 301 scripts/somefolder/script1.php$ https://newsite.website.com/script1.php$1
This is a much cleaner and simple method of performing a redirect for a single file while keeping all of the URL variables in place.