Skip to content
Advertisement

php72 dynamic url routing with front controller via entry point standard not working Google App Engine [GAE]

My dynamic URL is not reading on my website on GAE, cause I want to use the incoming GET to query my database.

As it stands, Php72 does not allowing routing from app.yaml must be from front controller via entry point which the default is publichtml/index.php or index.php.

I changed mine to worker.php. The listing.php URL below is the dynamic that I mean listing/random-dynamic-text-here while index.php and about.php is static pages.

NB: the page index.php and about.php and listing.php is called and displayed on the browser, but i cannot get the content of $_GET[“linkcheck”]; in listing.php. Refer below.

app.yaml

runtime: php72

runtime_config:
  document_root:
  
handlers:

- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301

entrypoint:
  serve worker.php 

//entry point worker.php

<?php 
ini_set('allow_url_fopen',1);
switch (@parse_url($_SERVER['REQUEST_URI'])['path']){
  case '/':
      require 'index.php';
      break;
  case '/index':
      require 'index.php';
      break;
  case '/index.php':
      require 'index.php';
      break;
  case '/about':
      require 'about.php';
      break;
  case '/about.php':
      require 'about.php';
      break;
  case '/listing':
      require 'listing.php';
      break;
  case '/listing.php':
      require 'listing.php';
      break;
  case '/listing/':
      require 'listing.php/';
      break;
  case '/listing.php/':
      require 'listing.php/';
      break;
  default:
      http_response_code(404);
      header("Location: https://www.example.com/404");
      exit();
}
?>

index.php

<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
</body>
</html>

about.php

<html>
<head>
<title>About</title>
</head>
<body>
<h1>About</h1>
</body>
</html>

Where listing.php below is the file/page where I am expecting $_GET[“linkcheck”];

listing.php

<html>
<head>
<title>Listing</title>
</head>
<body>
<h1><?php $cool = $_GET["linkcheck"]; echo $cool; ?></h1>
</body>
</html>

Advertisement

Answer

To achieve this you need to modify two of your files:

worker.php

<?php 
ini_set('allow_url_fopen',1);
$path = @parse_url($_SERVER['REQUEST_URI'])['path'];
switch ($path){
  case '/':
      require 'index.php';
      break;
  case '/index':
      require 'index.php';
      break;
  case '/index.php':
      require 'index.php';
      break;
  case '/about':
      require 'about.php';
      break;
  case '/about.php':
      require 'about.php';
      break;
  case '/listing':
      require 'listing.php';
      break;
  case '/listing.php':
      require 'listing.php';
      break;
  case '/listing/':
      require 'listing.php';
      break;
  case   (preg_match('/listing.*/', $path) ? true : false) :
      require 'listing.php';
      break;
   default:
      http_response_code(404);
      header("Location: https://www.example.com/404");
      exit();
}
?>

The difference here is to match regular expression that starts with /listing and send it to the listing Script.

The second modification is on listing to have this line:

<h1><?php $cool = $_GET["linkcheck"]; echo $cool; ?></h1>

replaced by this one:

<h1><?php $cool = $_SERVER['REQUEST_URI']; echo $cool; ?></h1>

I tested it and it works now as espected.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement