How to Use .htaccess, RewriteRule, and RewriteCond

Photo by Joshua Sortino
Photo by Joshua Sortino on Unsplash

Hey there, curious minds! Today, we’re going to explore the mystical world of .htaccess, RewriteRule, and RewriteCond. Don’t worry; I promise to keep it as simple as telling a bedtime story.

The Basics: What is .htaccess?

.htaccess is a special file that tells your web server how to handle URLs. It’s like a traffic cop for your website, directing visitors to the right places. But how does it work? Let’s break it down with some simple examples.

What about RewriteRule?

RewriteRule is where the real magic happens. It’s like a recipe that tells the web server how to transform URLs.

The basic syntax of a rewrite rule is RewriteRule pattern substitution [flags]. Here, the pattern is a regular expression that will match the URL(s) you want to rewrite, the substitution is the URL you want to redirect to when a match is found, and flags are optional parameters that can change the functionality of the rule

Let’s see RewriteRule in action:

RewriteEngine On
RewriteRule ^product/([0-9]+)$ /product.php?id=\$1 [L]

Explanation:

  • RewriteEngine On: Enables the rewriting engine.
  • RewriteRule: Specifies the rule for rewriting URLs.
  • ^product/([0-9]+)$: Defines the pattern to match in the URL.
  • ^product/: Matches the string “product/” at the beginning of the URL.
  • ([0-9]+): Matches one or more digits and captures them as a group.
  • $: Matches the end of the URL.
  • /product.php?id=\$1: Specifies the destination URL to rewrite to, where \$1 represents the captured group from the pattern.
  • [L]: Indicates that this is the last rule to process if the pattern matches.

With this rule in place, when a user visits example.com/product/123, the server will internally rewrite the URL to example.com/product.php?id=123, but the user will still see the cleaner URL in their browser.

Please note that to use RewriteRule, you need to have the mod_rewrite module enabled on your Apache server. You can wrap the Rewrite statements within IfModule:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^product/([0-9]+)$ /product.php?id=\$1 [L]
</IfModule>

And RewriteCond?

RewriteCond is like an additional rule that tells the web server when to apply RewriteRule. It’s like saying, “Only paint the car silver if it’s a sunny day.” RewriteCond adds that extra layer of control.

Here’s an example of RewriteCond in action:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^product/([0-9]+)$ /product.php?id=\$1 [L]

In this code, we’re saying, “If the requested filename doesn’t exist (!-f) and does not match an existing directory (!-d), then apply the RewriteRule.” This way, we only transform the URL if the file doesn’t already exist and is not a directory on the server.

Conclusion

And there you have itβ€”a simple guide to using .htaccess, RewriteRule, and RewriteCond. With these tools, you can make your website’s URLs friendlier, more organized, and even a bit magical. So go ahead and try it out!

Share: