Reputation: 408
My .htaccess is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php
and then, handling the url redirect is the file url_parser.php which is as follows.
<?php
// open file and get its contents in a string format.
$string = file_get_contents("../json/site_map.json");
// decode the json string into an associative array.
$jsonArray = json_decode($string, TRUE);
// add trailing slash to URI if its not there.
$requestURI = $_SERVER['REQUEST_URI'];
$requestURI .= $requestURI[ strlen($requestURI) - 1 ] == "/" ? "" : "/";
// split up the URL at slashes.
$uriArray = explode('/', $requestURI);
// select the last piece of exploded array as key.
$uriKey = $uriArray[count($uriArray)-2];
// lookup the key in sitemap
// retrieve the absolute file URL.
$absPath = $jsonArray[$uriKey];
// reformulate the URL.
$path = "../$absPath";
// include the actual page.
include($path);
?>
in order to test my php code, I replaced
$requestURI = $_SERVER['REQUEST_URI'];
by the following:
$requestURI = "/welcome";
and it worked perfectly. So I'm pretty sure that there is something wrong inside my .htaccess file. How can I change that?
Upvotes: 1
Views: 116
Reputation: 15045
Change:
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php
to
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php?url=$1
Then change $requestURI = $_SERVER['REQUEST_URI'];
to:
$requestURI = (!empty($_GET['url']))
? $_GET['url']
: ''; // no url supplied
WARNING: Do not pass user supplied values to include()
. Make sure the paths are checked against a proper whitelist, otherwise a malicious user can hijack your server.
Upvotes: 2