redirecting urls

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

redirecting urls

Post by kendall »

Hello,

I have a situation where by i have a list of folders in a http://www.thedomain.com/thefolder

i want to have a functionality where if someone typed http://www.thedomain.com/somefolder it will redirect them to http://www.thdomain.com/thefolder/somefolder

the trick is here i want to fool people into thinking that there is a

http://www.thedomain.com/somefolder when really its http://www.thedomain.com/thefolder/somefolder

can this be done php? or is this a server situation?

Kendall
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

If you're on Apache, look at mod_rewrite. It will make you have happypants.

It could also be done with some ugly repetitive PHP code, I suppose, but I don't even really want to think about it... and spoofing the URL wouldn't be possible, anyway.
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

I don't know how to do it on a server level, but for individual files (I use http://www.mikechase.ca/admin/ as a shortcut to http://www.mikechase.ca/admin.php), try this javascript:

Code: Select all

<script type="text/javascript">
<!--
&#123;
location="www.mikechase.ca/admin.php"&#125;
//-->
</script>
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Leviathan,

i don't think its really what i want...because there wouldn't be anything existing in the first place to plant a code...

Unipus,
do you think you can provide any links with documentation or proably expamples of similar done scenarios?
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

If you add:

RewriteRule ^folder1/ /folder1/folder2/ to your .htaccess

This will redirect any urls you need to, so for an example:

If your directory was http://www.thdomain.com/thefolder/somefolder
and wanted it to display content in http://www.thdomain.com/thefolder

you would add:

RewriteRule ^thefolder/ /thefolder/somefolder/ to your .htaccess

Hope this helps
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Another idea is to use the header(). Often used on some boards, wher you have an /image/ directory that you dont want a user to see.
The /image/-dir contains a index.php that only hold something like

Code: Select all

<?php
 header("Location: ../");
?>
...or variables that are set earlier in an included file.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Leebo,

actually leebo it may be the other way round see i have a directory

http://www.thedomain.com/thefolder/somefolder
i want let say someone types http://www.thedomain.com/somefolder
it will lead them to the http://www.thedomain.com/thefolder/somefolder

thus
this is something i want to do with all folders residing in the '/thefolder'

Am i ridiculaous here or can it really be achieved?

JAM,

i am not really sure what you mean...because want i want will involve having the server find the real location of a folder...e.g.

as i said above if i have http://www.thedomain.com/thefolder/somefolder...the thing is i don't want people going thru the whole /thefolder/somefolder so i want them to just type root/somefolder and have the server figure it out that they want to really root/thefolder/somefolder. as in real life root/somefolder does not exist...however root/thefolder/somefolder does....am i making my self more clear here?

the reason i want this is that i am going to sub-host websites so i am putting all sub-host websites in a /thefolder/ but i don't want them to have there address as http://www..../thefolder/thewebsite....that's 2 long...so i'm thinking let them just have the http://www.../thewebsite.

Note: there is a reason that i need to have them in 'root/thefolder/thewebsite' rather than 'root/thewebsite'

Kendall
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, in my example ../ is the same thing as the parent folder, hence mooving to a different host, using the same folder structure, will still lead you to the parent folder.

I'm not offering solutions, merely other ways to solve it, but you need to tweak them around abit yourselves, so that you learn, and so that it actually works in your code (that we have no idea of what it looks like).

To further elaborate the example:

Code: Select all

$url = 'www.example.com/foo/bar/example/sitename.php'; // example, can be taken from a $_SERVER variable.
$site = basename($url,'.php'); // sitename
header("Location: http://www.example.com/". $site ."/"); // forward user
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: redirecting urls

Post by McGruff »

kendall wrote:Hello,

I have a situation where by i have a list of folders in a http://www.thedomain.com/thefolder

i want to have a functionality where if someone typed http://www.thedomain.com/somefolder it will redirect them to http://www.thdomain.com/thefolder/somefolder
Kendall
Pardon me for stating the obvious but why not just put the $somefolders in site root?
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

JAM,

ok thanks...i was merely trying to comprehend what you were getting at....see the problem here is that

there is no variable to be passed
there isnt going to be any redirecting page

all im simply saying is this. lets say you build a website...you want to host it on my site

i give you http://www.mydomain.com/JAMwebsite

but the server directory is
root/websites/JAMwebsite
the url really is
now instead of someone typing
they can type
in there browser the end result being
http://www.mydomain.com/JAMwebsite redirects to http://www.mydomain.com/websites/JAMwebsites thus....www.mydomain.com/JAMwebsite directory structure is root/websites/JAMwebsite
McGruff,

the reason i am putting it in root/websites/JAMwebsite and not root/JAMwebsites/

is because i am building a kind of 'community thing' and want to devise some dynamic functions that interact with the server alot...also want to try keeping it together...its a bit hard to explain but im thinking if i shud have the website hosted in a root/websites/ rather than have it at the root
Post Reply