1

In my index.php, I control which template to require according to the action parameter.

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

In my other page, I assign the action in the href.

<a href=".?action=post&amp;postId=<?php echo $post->id?>">

When you hover the link, it will show you something like

...?action=post&postId=101

But I don't want to show it like that. Instead, I want to show it like

.../post/101

And in the href, I kinda use

<a href="./post/<?php echo $post->id?>">

My question is, how will then I be able to handle it in the index.php

iPhoneJavaDev
  • 1,111
  • 2
  • 22
  • 59

2 Answers2

1

Use .htaccess

RewriteEngine On
RewriteRule ^article/([0-9]+)\/?$  /page-to-load.php?action=post&postId=$1 [NC,PT,L]

With this example the server will take

www.example.com/article/101

and redirect it to www.example.com/page-to-load.php?action=post&postId=101

while retaining www.example.com/article/101 in the address bar.

Josh S.
  • 602
  • 3
  • 8
0

There are several ways to do this. Let's limit it to one method per answer: I pick PATH_INFO.

You can read up on the description of PATH_INFO in the cgi specification at https://tools.ietf.org/html/rfc3875#section-4.1.5

Basically the webserver, after deciding which resource to serve/which script to run, makes the additional path available through a variable called PATH_INFO. In PHP this usually ends up in $_SERVER['PATH_INFO'], see http://php.net/manual/en/reserved.variables.server.php

But you have to configure the webserver to accept and propagate this information.
For the httpd apache this is done by setting AcceptPathInfo On.

For other webservers it is done differently.

VolkerK
  • 92,020
  • 18
  • 157
  • 222