2

I am recently working on a php project. I created the template for my site and design the pages. However, the navigation between pages is like the following:

http://localhost:81/x/y/index.php and when I want to navigate to another page the URL will be like: http://localhost:81/x/y/second.php

how can I make the following URL takes me to the second.php page?

http://localhost:81/x/y/index.php?page=second

Thanks.

Ray
  • 781
  • 2
  • 17
  • 42

3 Answers3

2

Unless I am misunderstanding something, put this at the top of the index.php page:

<?php
if($_GET['page'] == "second")
{
    $newURL = 'http://localhost:81/x/y/second.php';
    header('Location: ' . $newURL);
}
?>

See How to make a redirect in PHP?

Community
  • 1
  • 1
Doug
  • 5,116
  • 10
  • 33
  • 42
1

I would add a bit more to it to make it secure but the simple answer would be...

if(isset($_GET['page'])
{
    require '/path/to/folder/' . $_GET['page'] . '.php';
}

If it is a redirection that you want, you could also do.

if(isset($_GET['page'])
{
    header('Location: ' . $_GET['page'] . '.php');
}
bluegman991
  • 707
  • 4
  • 9
  • I understood that by using $_GET['page'] you will get second and then you concatenate it with php. But I did not understand where to use this code – Ray Nov 11 '14 at 04:43
  • Yep, that way you don't have to add an if block for each page you want to access. You would just insert the code at the top of index.php, or preferably as the only code in index.php – bluegman991 Nov 11 '14 at 04:45
1

Try this Rule:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^x/y/index.php?page=(.*)$ http://localhost:81/x/y/$1.php [R=301,L]
Manwal
  • 23,450
  • 12
  • 63
  • 93