I'd like some help with some URL rewriting magic. I am having trouble modifying the Request_URI variable.

I would like to switch between two sites http://host.com/alpha and http://host.com/beta. They are both handled by the same php script. This script is http://host.com/index.php.

The index.php expects to be given a GET variable SITE to tell it which site to display. It also uses the REQUEST_URI to determine which content to display. In order for this to work, the alpha or beta need to be removed from the original request. This is where I am stuck.

So the REQUEST_URI starts at /alpha/content/file and needs to become /content/file.

I've tried this using mod_rewrite in .htaccess:

RewriteCond %{REQUEST_URI} /(alpha|beta)(.*)   
RewriteRule .* index.php?site=%1  


echo "Site: " . $_GET['site'] . "<br/>";
echo "Request_URI: " . $_SERVER['REQUEST_URI'] . "<br/>";

I'm hoping to have better luck with Apache's SetEnvIf and Alias.

Any ideas on how to do this would be greatly appreciated. Thanks.

Accepted Answer

Quite easy to manage:

RewriteRule ^(alpha|beta)/?(.*) /$2?site=$1 [R,L]

This will temporarily redirect (HTTP status code 302) any URLs beginning with "alpha" or "beta" to a resource without "alpha" or "beta" in the beginning but being appended as a query string, associated to the query variable "site".


// Will be redirected to http://host.com/shoes/nike/order.php?site=alpha
GET http://host.com/alpha/shoes/nike/order.php

EDIT This won't account for query strings provided by the original GET call. If you need those the following would do:

RewriteRule ^(alpha|beta)/?(.*) /$2?site=$1 [QSA,R,L]
RewriteRule .* index.php [NC,L]


Written by aefxx
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki