RewriteRule vs Redirect directive runtime behavior explained withexamples

RewriteRule




RewriteRule directive is used to rewite/redirect URLs on Apache server. To convert a longer URL path into shorter one we use RewriteRule directive which is part of Apache mod-rewite (Rewrite Module) .
The following is the basic syntax of RewriteRule directive:

RewriteRule newPath oldPath [flags]





Redirect directive




Redirect directive is part of Apache core module which is mainly used for URL redirection on Apache server. Redirect directive is used to redirect URL path from one location to the other for example /this to /that .

Syntax of Redirect directive:



Redirect  /newPath oldPath 




I often see users posting questions on stackoverflow about URL redirection. in their initial attempts to redirect the URLs they either try a mixture of RewriteRule and Redirect directives or misorder the directives which produces an unexpected result.



RewriteRule and Redirect are directives of two different apache modules. Although both the directives are used for URL redirection but due to their different run time behavior you can use them together in a single rule block.
RewriteRule directive takes precedence over Redirect directive. No matter where you place the Redirect if there is a RewriteRule directive with the same pattern as Redirect Apache server will check the RewriteRule first even if your Redirect line is at the top of your htaccess or server.config.



To better understand the runtime difference between these two directives, let's see some real world code examples now .

RewriteRule vs Redirect



The main difference between these two directives is ther runtime behavior. Both directives run on different time. Apache server often reads RewriteRule first.

Here is a basic htaccess code example to better understand the behavior :


Redirect /demo.php /location.php
RewriteRule ^demo.php$ /location.php [L,R]

The both lines above does the same thing as they redirect /demo.php to /location.php . As you can see the RewriteRule directive is placed bellow the Redirect in the above code, you would expect the Redirect to trigger first but The apache server will read the RewriteRule line first as this directive takes precedence over Redirect.



Intermixing RewriteRule with Redirect

Some people who are new to URL rewriting world often mix RewriteRule with Redirect. They use both directives together in htaccess hoping that the redirect will work normally this way but it doesn't. Why ? Because these two directives are part of two different apache modules.

Redirect /file.php /file1.php
RewriteRule ^file /file2.php [L,R]

In the above code we used Redirect directive to redirect /file.php to /file1.php and RewriteRule to redirect /file to /file2.php .

Since the pattern of above RewriteRule matches anything after file this will also match the /file.php request path so in the above code example our Redirect will never be triggered as the RewriteRule runs first and redirects any path starting with /file.

Post a Comment

0 Comments