简体   繁体   中英

.htaccess not redirecting successfully for pretty url's

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM