Jump to content

Help with mod_rewrite


Guest inlayz

Recommended Posts

Hi,I am new to this forum and this is my first post here.I couldn't find a category for Apache or servers, so I am posting it here (PHP is most synonymous with Apache, I believe!)My question is that can I use mod_rewrite to show pages from another server, yet making my visitors believe that they are actually on my server?For example, say I have a server abcd.com, but I want visitors to see content from xyz.com/asdf/index.php?id=256, but the browser's location bar should still display abcd.com.Is it possible using mod_rewrite? I am a bit confused if flag [P] can be used to achieve this or not.Please let me know if this can be achieved.Thanks

Link to comment
Share on other sites

PHP is most synonymous with Apache
Huh? Apache is a server operating system, and PHP is a programming language. However, apache servers commonly have PHP interpreters...mod_rewrite is an apache feature, not a PHP one.
Is it possible using mod_rewrite?
I don't think so, as as soon as you navigated away from the site the rule could not apply any more. You are better of creating a PHP proxy...
Link to comment
Share on other sites

You could map the URLs to a single PHP file and a single query string parameter (called "uri" in the sample below) in which you'll have

<?phpfile_get_contents('http://abcd.com' . $_GET['uri']);?>

This will fetch all contents from the remote site as is and show it to the user. If you need anything more though, you'll really have to use a PHP proxy. A PHP proxy is again a PHP file similar to this, but it processes the contents it fetches to ensure certain things to the user such as correct image paths, cookies and so on.

Link to comment
Share on other sites

Heh - like this super simple proxy I developed recently (it is rather hopeless, can handle internal relative links and some get-type forms, external stylesheets and scripts, but not absolute linking, post forms, cookies and sessions, or images:proxy.php

<?php	function rmv2slash($string) {		return str_replace(":/", "://", str_replace("//", "/", $string));	}	if (isset($_GET['address'])) {		$raw_address = $_GET['address'];		$address = urldecode($raw_address);		echo "<!-- Current page: $address -->\n";		$i = (strstr($address,"?")) ? true : false;		foreach($_GET as $get_key => $get_value) {			if ($get_key != "address") {				$address .= (($i) ? "&" : "?") . "$get_key=$get_value";				$i = true;			}		}		$address = rmv2slash($address);		$display_address = $address;		if ($page = file_get_contents($address)) {			if (substr($address,-1,1) != "/" && substr($address, 0, 1) != "?") {				$address = substr($address, 0, strpos($address, strrchr($address, "/")) + 1);			}			preg_match_all("/<link(.*?)type=\"text\/css\"(.*?)href=\"(.*?)\"(.*?)>/i", $page, $css_files);			foreach ($css_files[3] as $css) {				$page = preg_replace("/<link(.*?)href=\"" . preg_quote($css, "/") . "\"(.*?)>/i", "<style type=\"text/css\">\n" . file_get_contents($address . $css) . "\n</style>", $page);			}			preg_match_all("/<script(.*?)src=\"(.*?)\"(.*?)>(.*?)<\/script>/i", $page, $scripts);			foreach ($scripts[2] as $script) {				//echo "@@@ $script @@@";				$page = preg_replace("/<script(.*?)src=\"" . str_replace("/", "\/", $script) . "\"(.*?)>(.*?)<\/script>/i", "<script type=\"text/javascript\">\n". file_get_contents(rmv2slash($address . $script)) . "\n</script>", $page);			}			$page = preg_replace("/<a (.*?)href=\"(.*?)\"(.*?)>/i", "<a $1" . "href=\"?address=" . ((stristr("http://", "$2")) ? "$2" : "{$address}$2") . "\"$3>", $page);			$page = preg_replace("/<form (.*?)action=\"(.*?)\"(.*?)(method=\"get\")?(.*?)>/i", "$0<input type=\"hidden\" name=\"address\" value=\"{$address}$2\">", $page);			$page = preg_replace("/<form (.*?)action=\"(.*?)\"(.*?)>/i", "<form $1" . "action=\"" . substr($_SERVER['PHP_SELF'], 1) . "\"$3>", $page);			$page = preg_replace("/<body(.*?)>/i", "$0<div style=\"display:static; width:100%; background-color:#EEEEEE; padding:10px; margin-bottom:10px; text-align:center; font-family:arial; font-size:10pt; color:#000000; \"><form action=\"{$_SERVER['PHP_SELF']}\" method=\"get\" style=\"width:80%; \"><b>Super Simple Proxy</b><br /><input type=\"text\" name=\"address\" value=\"{$display_address}\" style=\"width:100%; \" /></form></div>", $page);			echo $page;		} else {			echo "Error: The page you requested could not be retreived.";		}	} else {		echo "<div style=\"display:static; width:100%; background-color:#EEEEEE; padding:10px; margin-bottom:10px; text-align:center; font-family:arial; font-size:10pt; \"><form action=\"{$_SERVER['PHP_SELF']}\" method=\"get\" style=\"width:80%; \"><b>Super Simple Proxy</b><br /><input type=\"text\" name=\"address\" style=\"width:100%; \" /></form></div>";	}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...