Redirect in .htaccess if condition is not true

Redirect in .htaccess if condition is not true

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Redirect in .htaccess if condition is not true

Redirecting in .htaccess When Condition is Not Met

When working with Apache configuration files like .htaccess, you may need to perform conditional redirects based on certain conditions not being met. In your case, you’re trying to redirect all requests from the /media/pdf/pds/ folder to its English subfolder when the English folder is not present in the request URI.

Here’s how you can achieve this using a properly structured .htaccess rule:

Understanding the Problem

Your current approach has a few issues:

  1. The RewriteCond negated condition (!^/media/pdf/pds/English/(.*)$) does not match the desired pattern.
  2. You’re using RedirectMatch 301, which performs an immediate redirect and stops further processing of rules in the .htaccess file.
  3. You didn’t include the RewriteRule to specify what should happen after the condition is met.

The Correct Solution

Here’s a properly formatted rule that should work:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/media/pdf/pds/English/(.*)$
    RewriteRule ^/media/pdf/pds/(.*)$ /media/pdf/pds/English/$1 [NC,L,R=301]
</IfModule>

Let’s break down this rule:

  • <IfModule mod_rewrite.c>: This ensures that the following rules are only applied if the mod_rewrite module is enabled.
  • Options +FollowSymLinks: Enables symbolic link following, which is required for rewriting to work correctly.
  • RewriteEngine on: Turns on the Rewrite engine.
  • RewriteCond %{REQUEST_URI} !^/media/pdf/pds/English/(.*)$: This condition checks if the request URI does not start with /media/pdf/pds/English/. The exclamation mark (!) negates the condition, so it matches when the English folder is not present.
  • RewriteRule ^/media/pdf/pds/(.*)$ /media/pdf/pds/English/$1 [NC,L,R=301]: If the previous condition is true:
    • ^ and $ define the start and end of the pattern to match.
    • (.*)$ captures any characters after /media/pdf/pds/ as a variable ($1).
    • [NC,L,R=301] specifies options for the rule: NC for case-insensitivity, L for last/stop processing if this rule matches, and R=301 for an HTTP 301 redirect.

Incorporating Additional Information

Regarding your additional information about the Magento installation and the presence of multiple .htaccess files:

  1. The main .htaccess file in the web root seems to be properly configured with mod_rewrite enabled.
  2. The htaccess file located in the media folder should not interfere with your rule, as it’s focusing on preventing directory listings rather than rewriting URLs.
  3. The new htaccess file you created inside the media/pdf/pds folder is correctly configured and should work seamlessly.

Conclusion

To summarize:

  • Always ensure that the Rewrite engine is turned on (RewriteEngine on).
  • Use appropriate conditions to match the desired pattern accurately.
  • Include a RewriteRule with the correct destination path and options (like case-insensitivity, last rule, and redirect type).
  • Place your condition and rule inside an <IfModule mod_rewrite.c> block for modularity and compatibility.
  • Ensure there are no conflicting rules or modules that might prevent your rewrite from working correctly.

By following these best practices and using the provided example as a reference, you should be able to successfully redirect requests in .htaccess when the specified condition is not met.

Extensions Deployment Errors

Our network