3

This is what I want to accomplish in pseudocode:

if server.host equals 'productionsite.com' then
  RewriteBase /
else if server.host equals 'stagingsite.com' then
  RewriteBase /staging/mysite
else if server.host equals 'localhost' or server.host equals '127.0.0.1' then
  Rewrite /dev/mysite

However, this is what I actually have so far which doesn't seem to work as I expect:

RewriteCond %{HTTP_HOST} =productionsite.com
RewriteBase /

RewriteCond %{HTTP_HOST} =stagingsite.com
RewriteBase /staging/mysite

RewriteCond %{HTTP_HOST} =localhost
RewriteBase /dev/mysite

I appreciate any help I can get in advance.

racl101
  • 3,760
  • 4
  • 34
  • 33

2 Answers2

1

Not an answer per-se (started out as a comment), but may help you a bit:

Your approach doesn't work because the RewriteBase isn't affected by RewriteCond directives. A RewriteCond can only be followed by another RewriteCond or a RewriteRule. RewriteBase is an entirely different beast: I don't think that a conditional RewriteBase is possible; I have the vague feeling that it's processed at startup, so can't be changed dynamically (but I may be very wrong about this). Google has a number of hits for "conditional rewritebase", you may look through those, but they seem largely negative.

Toying with IfDefine to achieve a similar effect would be worth a shot.

Alternatively, you could try doing it with RewriteRules and environmental variables:

 RewriteBase /

 RewriteCond %{HTTP_HOST} =stagingsite.com
 RewriteRule ^ - [E=FRB:/staging/mysite]

 RewriteCond %{HTTP_HOST} =localhost
 RewriteRule ^ - [E=FRB:/dev/mysite]

And then prepend each rewrite path with the FRB environmental variable, like

 RewriteRule ^home$ %{ENV:FRB}/index.php

But this is just an untested theory.

SáT
  • 3,633
  • 2
  • 33
  • 51
0

For me the solution from https://snipt.net/beat/htaccess-different-bases-subfolders-in-developmentproduction/ worked

RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/prod/]

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:/dev/]
michalzuber
  • 5,079
  • 2
  • 28
  • 29