March 15, 2021 0 Comments

Last Updated on April 12, 2021 by amitoverflow1

In this post we will learn about conditional URL rewriting on Apache server. This conditional URL rewriting tutorial is for mod rewrite beginners and advanced users who wish to learn how conditions work and how they are applied to
RewriteRule .

If you follow this short tutorial from top to the bottom then you will get a basic->advanced and complete reference about the conditional rewriting.

I wrote this post on a special request from one of my blog readers .
I hope you will enjoy reading it.

Conditions in RewriteRule (RewriteConds)

We use RewriteRule directive on Apache server to rewrite URLs .

Sometimes when we want to conditionally rewrite our URLs we use RewriteCond a condition directive along with RewriteRule to do the logical rewriting.
Whenever you open an .htaccess file to see its contents you often see a line that looks something like “RewriteCond blala blabla [blabla]” followed by RewriteRule . Have you ever wondered why the line is there and what its used for? . Reading this tutorial from the top , now you might have understood what the RewriteCond is used for.
Now let’s see some real world conditional URL rewriting examples so that you can understand how it actually works .

RewriteRule without conditions

RewriteEngine on
RewriteRule (.*) https://example.com [R]

The RewriteRule above works. You can just copy and paste it to your htaccess file and then visit any URL .
So what does it do?
This rule redirects your website traffic to https://example.com . If you type any URL of your website, you will get redirected to the example.com domain .
Now suppose , you don’t want one of your site pages to redirect. Or you want to redirect all your site pages except one to example.com.
Do we need to apply some logic to the rule here?
Yes, definitely! This is where we need to use the “RewriteCond” directive.

RewriteRule with condition

Now we have the same but with a condition :
RewriteEngine on
RewriteCond %{REQUEST_URI} !/demo.php [NC]
RewriteRule (.*) https://example.com [R]
If you test this rule on your server , and type demo.php file you will see that the rule does not redirect this file but all other URLs are still redirecting to example.com . This is because we have added a RewriteCond that says
“If the request is for “demo.php” then do nothing. Else run the Rewriterule” .
This is how If/else logic is applied to RewriteRule .
In the next section Bellow we will learn about RewriteCond Syntex .

RewriteCond syntex

RewriteCond directive uses a simple set of syntex. It takes 3 parameters with third as optional parameters.

RewriteCond testString pattern flags
Flags parameter is optional while the two other are required.
Test string can be any server variable you want to check against the pattern ie %{HTTP_HOST} .
Condition pattern is the regex based pattern you can use to check the variable.
Flag you can use mod-rewrite flags like [NC] which is used to perform a case sensitive match . This parameter is optional .

How to use use Rewrite conditions with RewriteRule

You can use multiple RewriteConds with a RewriteRule. Just keep in mind that the conditions are applied to RewriteRule that immediately follows.
Consider the following examples :

RewriteEngine on
RewriteCond %{REQUEST_URI} !/demo.php [NC]
RewriteCond %{REQUEST_URI} !/demo2.php [NC]
RewriteRule (.*) https://example.com [R]
In the code above we use two conditions and both the conditions are applied to the rule. The conditions above work like AND logic . The rule trigger only if the both conditions are met.

  • Request_uri is Not “demo.php”
  • AND

  • Request_uri is Not “demo2.php”

If both conditions are met then server will trigger the rule to redirect all other requests to example.com .
You can use [OR] flag to change the condition logic to OR.
RewriteEngine on
RewriteCond %{REQUEST_URI} !/demo.php [NC,OR]
RewriteCond %{REQUEST_URI} !/demo2.php [NC]
RewriteRule (.*) https://example.com [R]
Now the condition logic has changed to OR. The rule will trigger only if either the request_uri is “/demo.php” or “/demo.php” .

You can find good mod-rewrite reference on the official Apache server webpage :
https://httpd.apache.org/docs/current/mod/mod_rewrite.html .
I hope this article was helpful and you enjoyed reading it.

Leave a Reply

Your email address will not be published. Required fields are marked *