2013-07-18 125 views
1

我的.htaccess成功重定向如下:的.htaccess不漂亮的URL

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文件裏有什麼問題。我該如何改變它?

+0

您正在執行模式匹配並捕獲請求,但是您不會對其執行任何操作。 –

+0

你能詳細說明我應該怎麼做?這將非常有幫助。在此先感謝 – Muavia

+0

不清楚什麼是你的代碼不工作。 – anubhava

回答

2

變化:

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()。確保路徑是根據適當的白名單進行檢查的,否則惡意用戶可能會劫持您的服務器。

+0

它的工作!非常感謝你。 – Muavia