views:

56

answers:

1

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  

index.php:

<?php
echo "Site: " . $_GET['site'] . "<br/>";
echo "Request_URI: " . $_SERVER['REQUEST_URI'] . "<br/>";
//get_html($_SERVER['REQUEST_URI']);
?>

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.

+1  A: 

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".

Example:

// 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]

Cheers!

aefxx
yeah that's what I am looking for. I haven't got it work just yet tho. I'm seeing a 404 error..
xer0x
You would have to map the redirect to point to your index.php in your root directory (just a guess, I dont know your layout) with another rule. This would let you handle everything in your index.php and would have the correct REQUEST_URI to know what the client requested.
aefxx
I wasn't sure at first, but it's working now! Thanks!
xer0x
You're welcome.
aefxx