xer0x
xer0x

Reputation: 13156

How do I modify Request_URI using Apache or PHP?

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.

Upvotes: 1

Views: 4528

Answers (1)

aefxx
aefxx

Reputation: 25249

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!

Upvotes: 3

Related Questions