Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serveup a subdirectory (redirect)

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
US
I am very new to PHP so I do not know how to do what I need, or for that matter, if it can be done.

I have several sites hosted on a server that I have an account with. I have an index.php file in the root directory as shown below.

===========================================================
<?
if (substr_count ($_SERVER['HTTP_HOST'],"abc")!=0):
header("Location: [COLOR=red yellow]AAA[/color]/index.html");

elseif (substr_count ($_SERVER['HTTP_HOST'],"efg")!=0):
header("Location: [COLOR=red yellow]BBB[/color]/index.html");

elseif (substr_count ($_SERVER['HTTP_HOST'],"xyz")!=0):
header("Location: [COLOR=red yellow]CCC[/color]/index.html");

else:
header("Location:
endif;
?>
===========================================================

As you can see I am sending them to a sub directory, depending on how there got here.

If they typed then they will get the page at www.abc.org/[COLOR=red yellow]AAA[/color]/index.html
If they typed then they will get the page at www.efg.org/[COLOR=red yellow]BBB[/color]/index.html

If they type I want them to get www.abc.org/[COLOR=red yellow]AAA[/color]/test.htm

How can I put them in the right sub directory, and give them the page they need?
 
That would be best achieved with configuring the webserver and creating virtual hosts.
 
Well this being on a server where I do not have control of, I have to do it another way.

Could I hand off the info to a index.php in each subs and let that deal with the desired page? I would need to know the name of the page so I could work with it, but I do not know how to get that info and pass it on.
 
If this is an Apache server you could establish rewrite rules within an .htaccess file. That would be a fairly easy solution requiring no PHP coding at all.
 
Yes it is Apache server and I do have access to edit the .htaccess file.

What is this rewrite rules?

I know nothing about .htaccess functions.
 
I agree that the solution is better done at the web server level.

if you wanted to progress with your idea on your terms then you might investigate doing the following:

1. customise your 404 page not found error to point towards the index.php page (irony: you're already having to delve into apache here);
2. in the index.php file examine both the http_host to direct to the relevant sub-directory and also examine the $_SERVER['REQUEST_URI'] variable to get the sub-directory and page wanted. if you plan your directory schema properly you can just redirect on the http_host examination and reuse the request_uri value without change.

you might also consider "including" the resultant file rather than redirecting. doing the former would keep the url simple provided you were careful about your links in each resultant file (use $_SERVER['PHP_SELF'] to manufacture the links etc).

to repeat - this is all much better handled within apache but you can do (most of) it php if you really want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top