I don’t have access to the Apache server configuration files and can only influence the configuration through .htaccess files. Unfortunately, this rather limits some things one might want to do. For example, the easiest way to redirect all HTTP requests to HTTPS would be:

<If "%{HTTPS} != 'on'">
    Redirect permanent / https://www.my-domain.com/

However, this forces me to hard-code the domain name, so I can’t reuse this .htaccess file easily. In my case, the same site can be accessed from several different domains (with only one underlying filesystem), so this is not an option for me at all. I can’t solve this by redirecting to https://%{SERVER_NAME}/ since the Redirect statement doesn’t support this kind of variable substitution (except in <Location> or <LocationMatch> sections, but of course, these are not available to .htaccess files).

So already, if I want to have a general/reusable .htaccess configuration, I’m forced to use mod_rewrite instead of Redirect so I can use server variable substitution. The corresponding solution is

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=permanent,L]

which is already significantly more complex, not to mention possible interactions with other mod_rewrite rules I might want to set up – preferably, I would like to keep use of mod_rewrite to a minimum due to its complexities.

Another example, for which I haven’t found a workaround at all, is mod_substitute, which doesn’t support %{variable} expansion at all. If my site is in a specific subdirectory (sub_dir) on the server and I want to post-process all links on HTML pages in that directory, I can do

AddOutputFilterByType SUBSTITUTE text/html
Substitute s~/sub_dir/[^\s]+/~/sub_dir/something_else/~

so here, I have to hard-code the directory sub_dir, which I would have to replace for every other subdirectory I might want to perform the same substitution in (no, I don’t have access to the root URL /). If this were a rewrite rule, I could just define an environment variable %{env:site_dir} like so:

RewriteRule ^ - [E=site_dir:/sub_dir]

but this environment variable is of no use to me because mod_substitute doesn’t interpret these.

So, is there any way to avoid hard-coding and duplicating domains and subdirectories like this in modules like mod_alias or mod_substitute, which don’t have the same mechanisms as mod_rewrite? For example, by somehow getting access to environment variables like %{env:variable} in these modules?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.