2013-01-06 156 views
0

考慮這樣的工作流程:PHP:搜索目錄樹的文件,並返回文件路徑

用戶對website.com/lolmyblogpost

我.htacces是所有喜歡的請求......

RewriteCond %{REQUEST_URI} !=/index.php 
RewriteRule .* /index.php 

在哪裏index.php即時查找模板文件樹lolmyblogpost.html返回:

/path/to/lolmyblogpost.html 

所以在我的主模板,我可以:

{include file="{$pathToTemplate}"} 

如何搜索目錄樹的文件,並返回文件路徑?

+1

好的。所以你的問題是什麼!!!!!!!!!!!!!!! –

+0

@mamdouhalramadan問題是我如何「PHP:文件和返回文件路徑的搜索目錄樹」 –

+0

在Apache,PHP,Ruby,...什麼語言? – Tigger

回答

1

你真正想要的是「回顧」支持與默認類型。設置你的Apache虛擬主機(東西)是這樣的:

<VirtualHost *:80> 
    ServerName example.com:80 
    ServerAlias www.example.com 
    DocumentRoot "/path/to/root/dir" 
    AddDefaultCharset UTF-8 
    ErrorDocument 403 "/403.php" 
    ErrorDocument 404 "/404.php" 
    <Directory /path/to/root/dir> 
      Options Indexes FollowSymLinks 
      DefaultType application/x-httpd-php 
      AllowOverride All 
      Order deny,allow 
      Allow from all 
      AddOutputFilterByType DEFLATE application/javascript text/css text/html text/plain text/xml 
      RewriteEngine On 
      RewriteBase/
      RewriteCond %{REQUEST_FILENAME} !.index.ph.* 
      RewriteRule ^(.*)$ /index.php 
    </Directory> 
</VirtualHost> 

的重要行是DefaultType application/x-httpd-php,這意味着你現在可以擺脫.php文件擴展名。

您現在可以使用一個URL,如http://example.com/this_is_a_php_page,您也可以使用http://example.com/this_is_a_php_page/with_a_path_info_var

因此,在this_is_a_php_page(這實際上是一個沒有擴展名的.php文件),您可以使用$_SERVER['PATH_INFO']來檢查在URI中傳遞的變量和變量。

編輯: 添加了RewriteEngine和規則,推動一切index.php。這意味着您現在在服務器上有一個名爲index.php的(實際)單個頁面,它將需要檢查$_SERVER['REDIRECT_URL'] var的真實請求。

例如,對http://example.com/a_page的請求現在將加載index.php並將a_page傳遞給$_SERVER['REDIRECT_URL']。注意:這個解決方案會將所有內容都推送到index.php。您將需要包括像一個例外:

RewriteCond %{REQUEST_FILENAME} !.not_me_plz.* 

要允許與not_me_plz文件開頭的文件送達「預期」。

+0

如果我試圖保留'example.com/singlepathtofile',該怎麼辦?想象一下,在文檔根目錄下,我的文件樹可能看起來像「2012/08,09,10/singlepathtofile」。html' ...'2013/01/singlepathtofile.html,02,03'等 –

+0

作出更新以回答您的問題。 – Tigger

+0

這部分'2012/08,09,10 /'是非常混亂(很難處理)。爲什麼不有一個像這樣的URI:'http:// example.com/title_of_post/2012/08,09,10'或'http:// example.com/2012/08,09,10/title_of_post'這是很容易的處理。 – Tigger

相關問題