3

I've got this in httpd.conf:

<VirtualHost xx.xxx.xx.xxx>
    Options All +ExecCGI
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/domain.com
    ServerName  dl.domain.org
    DirectoryIndex dlindex1.html
</VirtualHost>

... which is fine (what I need as the DirectoryIndex for our 'dl.domain.org' subdomain), but now I also need to alter that DirectoryIndex based on IP address, using .htaccess. Is this possible?

StackOverflow posts are telling me that I cannot set DirectoryIndex conditionally.. but instead have to use a RewriteRule. If that is true, OK, but what RewriteCond and RewriteRule? I have tried many things, including (where the actual IPs are those of our 2 devs):

RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$    [OR]
RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteCond %{SERVER_NAME} ^dl.domain.org
RewriteRule ^(.*)/$ $1/dlindex2.html

..or even just (as an absolute test):

RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteRule (.*)/dlindex1.html$ $1/dlindex2.html

But it seems that whatever I try it just serves up the DirectoryIndex dlindex1.html as per httpd.conf, as opposed to the dlindex2.html I want served up as the default page in that subdomain when a devs IP is calling.

Can any one point me to what I can do to get what I am after? i.e. this: ...to actually, or even just effectively, alter DirectoryIndex based on IP address, using .htaccess, on the fly?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
govinda
  • 153
  • 1
  • 8

1 Answers1

2

Assuming we want the IP address 12.34.56.78 to go to /var/www/client1/* and we want the IP address 87.65.43.21 to go to /var/www/client2/*

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.78$
RewriteRule (.*) /client1/$1

RewriteCond %{REMOTE_ADDR} ^87\.65\.43\.21$
RewriteRule (.*) /client2/$1

Make a set of "RewriteCond" and "RewriteRule" for each IP you need to serve a special page for.

Operation break down:

  1. Client 87.65.43.21 connects
  2. Rule checks if the client is 12.34.56.78, it is not
  3. Server then moves on to the second condition
  4. Checks if the client is 87.65.43.21, it is
  5. Server now executes the RewriteRule that is part of the conditional that has passed the test, and the page /client2/(file_requested) is served.

The OP has clarified his question, he doesn't need separate folders, but different index pages to be served, here is an example .htaccess for that job:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index-page-client1.html

RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index-page-client2.html

The above rule will additionally check if the file requested is/was "index.html", I assume you only want to redirect requests for "index.html"

Tim
  • 6,113
  • 1
  • 18
  • 19
  • Tim, that is a good clear example.. thanks! ..but I do not need to serve up a different directory, conditionally.. but rather just a different `defaultIndex`, IP-conditionally. At first (noob) glance at your sample code.. if I used that.. it would affect even traffic that was directed to other (sub)?domains on our site. I need a custom/dev IP-induced `defaultIndex` for only a specific subdomain. – govinda Jul 27 '12 at 17:43
  • @govinda I added an example for the situation you have outlined. If you need the URI check to only check a subfolder, change `^index\.html` to `^yoursubfolder/index\.html` – Tim Jul 27 '12 at 17:52
  • Tim, as per your suggestion, I just tried this: `RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$ RewriteCond %{REQUEST_URI} ^dlindex1\.html RewriteRule .* /dlindex2.html` ...but it seems to have no effect at all. IOW, it seems that the ` ServerName dl.domain.org DirectoryIndex dlindex1.html` from `httpd.conf` is the only thing Apache is listening to here. Is it because `%{REQUEST_URI}` did NOT contain 'dlindex1.html' before that `DirectoryIndex` kicked in? ; you see I am trying to effectively override the `DirectoryIndex dlindex1.html` from `httpd.conf`. – govinda Jul 27 '12 at 20:02
  • Where are you placing these directives? Also, that remote address, does not seem to be real, so it would never match for you, defaulting to DirectoryIndex you specified previously (in this case, in httpd.conf) (If you are censoring the IP address forgive me!) – Tim Jul 27 '12 at 20:40
  • I am placing the directives inside ` RewriteEngine On RewriteBase / ... ` ...alongside another `RewriteCond`+`RewriteRule` that works fine (to pass-protect a private directory) ...in .htaccess. Yes, I just stuck a dummy IP. I tested other (successful) `RewriteCond`+`RewriteRule`s with my IP, so I know the IP is not the issue. To be sure I even tried `RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$ RewriteCond %{REQUEST_URI} ^dlindex1\.html RewriteRule .* /dlindex2.html ` ...but to no effect (IOW the `RewriteRule` never kicks in). – govinda Jul 27 '12 at 21:01
  • 1
    @govinda I suggest trying the example in a new .htaccess files. Also note I caught a mistake, my regex for REQUEST_URI had a leading carrot `^`, I removed that and placed a `$` at the end. You could also remove the REQUEST_URI restriction completely, to debug it. – Tim Jul 28 '12 at 15:54
  • Tim, that did it! So simple, but it took your skill to catch that last detail that made the difference. Thanks. One note- the first (or second) block in your latter example needs a NOT (!) before the second IP (e.g. `RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$`) – govinda Jul 29 '12 at 14:26