1

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.

1
  • You need a framework. Commented Nov 11, 2014 at 4:31

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

what if i have 8 pages and you can access them from any page. This solution will be repeated 8 times in each page. Am I right?
Then put the code in one library file and call include() to include it in each page, so you don't have to rewrite it 8x.
or <?php if (isset($_GET['page']) { if(htmlspecialchars($_GET['page'])=='second') { include('second.php') return; } } ?>
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');
}

2 Comments

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
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
1

Try this Rule:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.