4

I can't get it work this part of .htaccess, the IfDefine never runs. What am I doing wrong, setenvif mod is enabled.

RewriteBase /

SetEnvIf HTTP_HOST ^localhost$ local
<IfDefine local>
  RewriteBase /codeigniter-app/
</IfDefine>

SetEnvIf HTTP_HOST ^testing.alex.com$ testing
<IfDefine testing>
    RewriteBase /app/
</IfDefine>

This is based on: With mod_rewrite can I specify a RewriteBase within a RewriteCond?

Edit: Any other way to accomplish the above?

Community
  • 1
  • 1
Alex
  • 7,538
  • 23
  • 84
  • 152

4 Answers4

12

SetEnvIf works on request headers. So you want to use HOST not HTTP_HOST. E.g.

SetEnvIf HOST ^localhost$ local
emarref
  • 1,286
  • 9
  • 18
  • I tried using the word localhost but it does not work on my apache server, so I have to replace it for each website I havewith the hostname, ex: website.com. I wonder if there is any variable like HTTP_HOST that can be used. – Amr Jun 17 '21 at 00:54
5

SetEnvIf sets an environment variable. It doesn't define a new constant for Apache to use.

See the SetEnvIf manual and compare it to <IfDefine> manual.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • this is where I read about doing so: http://stackoverflow.com/questions/2045897/with-mod-rewrite-can-i-specify-a-rewritebase-within-a-rewritecond – Alex Feb 09 '12 at 14:18
  • 1
    can you suggest any possible fix/solution to my problem? – Alex Feb 09 '12 at 14:45
4

My answer if that Apache doesn't allow you to do what you are trying to do here, but that's because AFAIK you don't need to do this to do what you really need to do.

It seems as if you are trying to create a local environment that in configuration terms mirrors your (remote) server-based test environment. However you are confusing the URI namespace and the file system namespace and in Apache Server these are quite different. For example the <Location> directive operates on the former and the <Directory> directive operates on the latter. .htaccess sort of cut across both so you need to understand when a parameter operates on one or the other.

For example, I have a shared service account which service a number of subdomains *.ellisons.org.uk -- for example my blog is on http://blog.ellisons.org.uk/. I develop on an Ubuntu laptop and a VM that mirrors the LAMP/suPHP configuration that my service provider operates (but I have root access and can see the rewrite logs, etc. on the VM) I statically allocate IP addresses in my 192.168.1.0/24 private address space and have /etc/hosts symonyms for

127.0.1.1      ... blog.ellisons.org.home forum.ellisons.org.home ...
192.168.1.250  ... blog.ellisons.org.vm ...

But other than this the URI space is identical,so blog.ellisons.org.XX/themes/terry/all.css loads my css from my server,laptop or VM depending on whether XX=uk,home,vm

However the docroot on my laptop is Debian standard /var/www, but on the other two it is /websites/LinuxPackage02/el/li/so/ellisons.org.uk/public_html (my hosters naming convention). Yet my application including .htaccess files is identical, and the only way that these reflect this multiple use is in regexps where I using as a regexp to match %{HTTP_HOST} (\w+)\.ellisons.\org\.(?:uk|home|vm).

So to your example:

  • There's nothing to stop you pointing the requests to /app to your physical directory /codeigniter-app on your local PC. You can use an Alias directive in your server or vhost config to do this..
  • RewriteBase takes a URI path argument so /app would work in both.
  • In general Apache understands the difference between URI and FS domains and doesn't get confused -- even your developers regularly do :-) On the rare occasions that you need to refer explicitly to a FS name, say in a RewriteCond string, you can use %{DOCUMENT_ROOT} -- though if your service is a shared service you may have to use an environment variable instead (mine is %{ENV:DOCUMENT_ROOT_REAL}). I phpinfo() will tell you which.
  • Make sure that you use relative addresses in any RewriteRule substitutions (no leading slashes).

So to summarise, with a little bit of thought you can make your configurations logically identical as Apache offers -- isn't that better than trying to botch in conditional execution in a way that was never designed into the product?

TerryE
  • 10,724
  • 5
  • 26
  • 48
  • localhost, testing site (different host) and live site (rewritebase: /). So apache aliases won't work. If you're suggesting something like: http://stackoverflow.com/questions/2434820/many-rewritebase-in-one-htaccess-file please advice me to apply it to my codeigniter .htaccess https://gist.github.com/d11be61ebca9bc49daab – Alex Feb 09 '12 at 23:22
  • +1 for your extensive information, I'm going to try this:http://serverfault.com/questions/53145/mod-rewrite-and-multiple-rewritebases – Alex Feb 09 '12 at 23:43
  • "localhost, testing site (different host) and live site (rewritebase: /)" exactly. I assume that you have root access to localhost and the testing site, so you use Aliases in their vhosts so that the URI (Location hierarchies) are the same for all 3, then you don't need to have different rewrite bases. That's what I do and my live config is _very_ different from localhost. – TerryE Feb 10 '12 at 10:08
  • What I don't understand is how it would work if in localhost I have more than `app` and also on `testing`? – Alex Feb 10 '12 at 22:10
  • Notice above that I have lots of synonyms for localhost e.g. blog.ellisons.org.home. See the Apache doc [Named Based Hosts](http://httpd.apache.org/docs/2.0/vhosts/name-based.html). You just do the same and have one dummy subdomain per app, and establish a different DocumentRoot or Alias sets for each. – TerryE Feb 11 '12 at 00:11
  • I've had a look as yr git `.htaccess`. You are still hopelessly confusing URI and FS namespaces. In a Per Dir context, both the regexp and replacement string work in a URI context, but you are trying to use them in a FS one. You are really making life awfully complicated for yourself. Use relative addressing and with a bit of thought you can get rid of this pseudo-conditional stuff. – TerryE Feb 11 '12 at 15:49
  • only what's in "Start rewrite engine" section was added/modified by me, the rest comes from html5bp... and that `.htaccess` is used on the production site/ – Alex Feb 11 '12 at 19:49
  • Yup I know, I've just diffed yr current and i-2 versions against the html5bp version. It's the "Start rewrite section" that I am talking about. At least this explains one confusion: I could see how the author of the rest of this htaccess file would have made this mistake. I keep coming back to the Q that you _should_ be asking yourself: How can I configure my localhost environment so that I can use the prod `.htaccess` unchanged? ***Not*** how do I do something that Apache was never designed to do? – TerryE Feb 11 '12 at 20:19
  • Well, for now, until I have time to better document and read more about .htaccess I decided to just use different `.htaccess` for each environment and it works great – Alex Feb 11 '12 at 20:35
  • Good exchange. I did the same, but because I also use git and have a unified release process for promoting to test and to prod -- in the end I gave up and fixed the dev & test environment so they could be identical. -- It was just too much as a pain in the a\*\*e. :LoL: – TerryE Feb 11 '12 at 20:45
0

You also need to make sure so that the .htaccess file is actually used by adding the following for your virtual host configuration:

DocumentRoot /var/www/html

<Directory /var/www/html>
AllowOverride All
# Other stuff
</Directory>

The AllowOverride All directive makes it possible to override configuration directives from an .htaccess file on a per directory basis.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93