Last Updated on April 18, 2021 by Amit
Redirect and RedirectMatch both directives are part of Apache core modules which means that you don’t need to install anything on your server to use these directives as they come pre installed with your server.
Redirect and RedirectMatch are used for external URL redirection. The difference between these two directives is that the Redirect uses a fixed path segment while the RedirectMatch uses dynamic pattern as it supports regex.
You can use a regular expression based pattern in RedirectMatch which makes the redirection easier and just one line one redirectMatch can be used to thousands of URLs.
Redirect directive and its uses
Redirect directive takes 3 parameters :
Redirect StatusCode /oldPath /newPath/URL
- StatusCode can be any redirect status starting from 3XX . For example you can use 301 to do a permanent redirection from your old URL to the new one.
This parameter is optional in Redirect directive. If no status is provided then the default is 302 (Temp redirect) which is used by the directive automatically if no status is found.
- The second parameter of Redirect is /oldPath . This must be an URL path starting with a slash. You can’t use full URL that start with URL scheme and host name. This is URL path you want to redirect from .
- The last perameter is the destination of the Redirect. The is the URL or path you will redirect your old URL to. You can use full URL or just path if the redirection is on your own domain.
You can use Redirect directive to redirect your URLs from one location to the other. This is a very simple method to redirect your URLs.
The following examples redirects yoursite.com/demo.php to example.com/hello.php
Redirect /demo.php https://example.com/hello.php
As you can see, we ommited the status perameter, this will use a temp Redirect status 302 to redirect the request.
Now if you wish to do a permanent redirection, then use can use
Redirect 301 /demo.php https://example.com/hello.php
If you want to redirect a particular directory and all its files and subdirectories , you can just use one liner redirect , something like the following:
Redirect 301 /dirName/ https://example.com/
The pattern above not only matches the /dirName/ string but it also matches anything that comes after /dirName/ for example /dirName/fooBar . You can use RedirectMatch if you just want to redirect a particular directory.
RedirectMatch is used to redirect URLs same as Redirect but this directive supports regular expressions in its pattern which makes it unique and a simple to use redirect method.
RedirectMatch directive takes three perameters
- Status code (optional)
- Old path
- New URL/Path
The status code is optional same as Redirect. If no status code is used then URL redirection defaults to 302 which is a temporary redirect.
In the old path perameter you can use a regular expression based pattern.
Examples of RedirectMatch
Suppose we have a directory named “fooBar” in our public_html folder, and we want to redirect this directory and all its contents to another directory named “blabla” on the same server, we can use the following code to achieve this :
RedirectMatch 301 ^/foobar/(.*)$ /blabla/$1
The code above redirects /foobar/ and /foobar/anyThing to /blabla/anything .
This is the regex magic that matches the URL path dynamically and saves the match in $n variable. This saves your time otherwise you would have to write multiple Redirect lines to redirect all URLs.