Is there a way to redirect pages?

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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Is there a way to redirect pages?

Post by ljCharlie »

I was wondering if there is a way to redirect pages. For example, I have the following pages:

page1.php
page2.php
page3.php

When people typed in or link from other pages to the following:

http://www.mydomain.com/page1.php, I want to redirect to mypage.php?id=1
http://www.mydomain.com/page2.php, I want to redirect to mypage.php?id=2
http://www.mydomain.com/page3.php, I want to redirect to mypage.php?id=3

Is this possible?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well you can do a few things. you could do a mod rewrite (i think but i have never done one so i don't know for sure) and you can do this (the easy way)!

Code: Select all

switch ($_GET['id'])
{
  case 1:
    header("Location: http://www.website.com/page1.php");
    break;
  case 2:
    header("Location: http://www.website.com/page2.php");
    break;
  case 3:
    header("Location: http://www.website.com/page3.php");
    break;
  case 4:
    header("Location: http://www.website.com/page4.php");
    break;
}
EDIT: sorry I read that backwards as you can tell. Mod Rewrite is what you are looking for.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thank you for the response. Any source on how to rewrite the Mod?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

By the way, is mod rewrite requries access the apache web server? If it is then this is not an option because I don't have access to the apache web server.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

not true, mod_rewrite may be enabled (it is quite popular, so likely to be enabled). and you can use a .htaccess file to do it!
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Okay. I'm looking at my web folder and I do see the .htaccess file. I open this up and saw this:

ErrorDocument 404 /errors/404.php?emailaddress=myemail@mydomain.com

Is this mean I can use .htaccess to redirect missing pages to other current pages? If so, how do I do it? Resource?
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Okay, here's what I tried.

RewriteEngin On
RewriteBase /
RewriteRule ^scholarships\.php$ foundAll.php?id=1

The error is:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, myAdmin@mydomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.0.54 (Win32) PHP/5.0.4 Server at http://www.mydomain.com Port 80
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Can't remember off by heart.

But i'm 99% sure this should work for you:

Code: Select all

RewriteEngine on
RewriteRule ^page([0-9]).php$ mypage.php?page=$1
That should make any of the following pages goto the corrosponding url (NOT A VISIBLE REDIRECT, user will still think he is on page*.php)

Code: Select all

page0.php -> mypage.php?page=0
page1.php -> mypage.php?page=1
page2.php -> mypage.php?page=2
page3.php -> mypage.php?page=3
page4.php -> mypage.php?page=4
page5.php -> mypage.php?page=5
page6.php -> mypage.php?page=6
page7.php -> mypage.php?page=7
page8.php -> mypage.php?page=8
page9.php -> mypage.php?page=9
I suggest you use something like:

Code: Select all

RewriteEngine on
RewriteRule ^pages/(.*).php/$ mypage.php?page=$1
This will make something like this happen:

Code: Select all

/scripts/purchase.php -> mypage.php?page=purchase
/scripts/download.php -> mypage.php?page=download
/scripts/adminlogin.php -> mypage.php?page=adminlogin.php

I hope this helsp you.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thank you so much for the hlep. I still receive the error. I tried just RewriteEngine On and still receive the error. So I guess that means mod_rewrite is not turned on. If this is true, what are my other options if any?
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

It could be a sign that it is disabled.
Your only other bet is to create a php file for every page and whack:

Code: Select all

<?php
$PAGE_NAME = ""; // This can be replaced with some code to auto get it, forgot the code tho.
header("Location: ../mypage.php?page=".$PAGE_NAME);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

might be worth it to ask your host if they have mod_rewrite on, or if you php supports it, you can interogate Apache yourself: apache_get_modules()
Post Reply