0

I'm trying to redirect to a URL if a test for a module passes.

Something like this (Which doesn't seem to work for me.)

Perhaps this just isn't possible. Anyone?

<IfModule mod_rewrite.c>
    <ifModule mod_gzip.c>
        RewriteEngine On
        RewriteRule ^/mod_gzip/?$ mod_gzip-is-enabled.php
    </IfModule>
</IfModule>
2

Nested <IfModule> containers are not your problem (yes, this is possible). (But is the outer <IfModule mod_rewrite.c> container necessary?)

Your problem is the RewriteRule pattern ^/mod_gzip/?$:

RewriteRule ^/mod_gzip/?$ mod_gzip-is-enabled.php

This will never match in a per-directory .htaccess context, because of the slash prefix on the RewriteRule pattern. You need to remove it, like so:

RewriteRule ^mod_gzip/?$ mod_gzip-is-enabled.php [L]

In order to match a requested URL of the form /mod_gzip/ (or /mod_gzip).

In per-directory .htaccess files, the URL-path that the RewriteRule pattern matches against is less the directory-prefix, which notably ends with a slash. So the URL-path that is matched never starts with a slash. (This differs from when the directives are used in a server or virtualhost context.)

Also, you should probably include the L flag in case you have other directives.

  • "But is the outer <IfModule mod_rewrite.c> container necessary?" Well I figured it was because if mod_rewrite.c isn't available then Rewrite atempts would fail(?) I tried your code but visiting [domain].com/mod_gzip is just redirecting to / as if mod_gzip-is-enabled.php doesn't exist (Which it does.) – Jay Mar 21 '18 at 22:04
  • Whether the outer <IfModule> that checks for mod_rewrite is required is dependent on whether the ability to rewrite the URL is mandatory or not? If you are OK with the process silently failing regardless of whether mod_gzip is enabled then keep the check for mod_rewrite. If you are seeing a "redirect to /" then something else is triggering that - there is no "redirect" here, it's an internal rewrite. (If mod_gzip-is-enabled.php didn't exist and this directive was executed then you'd expect to see a 404.) Do you have other directives in your .htaccess file (or server config)? – MrWhite Mar 26 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.