簡體   English   中英

.htaccess無法成功重定向漂亮的URL

[英].htaccess not redirecting successfully for pretty url's

我的.htaccess如下:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ ./backend-scripts/url_parser.php

然后,處理url重定向的是文件url_parser.php,如下所示。

<?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);

?>

為了測試我的php代碼,我更換了

$requestURI = $_SERVER['REQUEST_URI'];

通過以下方式:

$requestURI = "/welcome";

而且效果很好。 因此,我很確定.htaccess文件中有問題。 我該如何改變?

更改:

RewriteRule ^(.*)$ ./backend-scripts/url_parser.php

RewriteRule ^(.*)$ ./backend-scripts/url_parser.php?url=$1

然后更改$requestURI = $_SERVER['REQUEST_URI']; 至:

$requestURI = (!empty($_GET['url']))
    ? $_GET['url']
    : ''; // no url supplied

警告:請勿將用戶提供的值傳遞給include() 確保根據正確的白名單檢查路徑,否則惡意用戶會劫持您的服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM