The best way to redirect?

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
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

The best way to redirect?

Post by fastfingertips »

I want to redirect someone to another page, can you give me a code example on how to do that?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

header('Location: somepage.php');
[php_man]header[/php_man]
It should be called before any output...
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

you can also use HTML features to redirect user from one page to another

Code: Select all

<meta http-equiv="refresh" content="1;url=your_url">
-you can also adjust seconds to redirect the user
this example you can see on this forum when you post a reply it stays 3 second after posting, saying "your message has been posted...." and then redirects you to original thread from where you came
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Not to mention JavaScript
<SCRIPT LANGUAGE="JavaScript">
<!-- Change location to suit your needs below
window.location="http://www.yourdomain.com/";
// -->
</script>
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

^ what if javascript is disabled in browser
-i think if redirection is not done by browser, then user should be given a link to the directed page.
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

what about when...

Post by tdelobe »

I have a case where I need to have a whole bunch of "user-friendly" urls that each redirect to a different no user-friendly url on my site. for example....

www.mysite.com/contact redirects to

www.mysite.com/index.php?page=home.contact

That way people can send out easy urls in emails or print docs where someone needs to type a url in.

So basically, I would like to put some code at the top of my controller template that sniffs out the url a user types in and if it is one of the ones I designate (like say the one above) than it redirects to the appropriate page.

Anyone know the best way to accomplish this task?[/b]
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Aha. You want mod_rewrite on your server (assuming you're using Apache as your server).

You might find this article has some helpful information: http://www.alistapart.com/articles/succeed/

And here's a URL rewriting guide that's helpful:
http://www.engelschall.com/pw/apache/rewriteguide/
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

sorry....

Post by tdelobe »

....sorry, didnt provide enough info on my situation. i am usings php 4 on windows IIS. so the apache thing in not an option. any other ideas?
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

isapi rewrite

Post by tdelobe »

ok, so i found this for IIS.

http://www.isapirewrite.com/

however, is there anyway to this this with php or javascript?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

The ALA link I posted has some information that might get your brain turning. Most of the work in their example is done in PHP. But yeah, it can mostly be done by PHP... search for PHP URL rewriting or somesuch.
Post Reply