2009-07-08 160 views
1

謝謝大家的耐心和幫助。我完全重申了這個問題,因爲我的所有修改都會變得很長。 我有4個的入口點的PHP MVC框架:URL重寫幫助

從根:
的index.php
索引ajax.php
管理員/ index.php的
管理員/索引-ajax.php

我需要一個.htcaccess文件,它可以接受任何請求,並根據url將其重寫到相應的文件中。長的網址是index.php?rt = cms/view/15,我希望它是index/cms/view/15。除了一個問題外,這一部分已經完成了。

這是我現在的.htaccess文件:

# htaccess file for framework - GOOD 
Options +FollowSymLinks 

# Turn on the mod_rewrite engine - GOOD 
RewriteEngine On 

# Hide indexes - GOOD 
Options -Indexes 

# If a file is not one of these, continue processing. - GOOD 
RewriteRule \.(css|js|jpg|jpeg|png|gif|ico)$ - [L] 

# RewriteRules for folder index files 
#RewriteRule ^(index)?(.php)?$ index.php [L] - GOOD 
#RewriteRule ^admin(/?)(index)?(.php)?$ admin/index.php [L] - GOOD 

# RewriteRules for admin folder arguements - going from more specific to less 
RewriteRule ^admin/ajax/[A-Za-z0-9-_/]*$ admin/index-ajax.php?rt=$1 [L] 
RewriteRule ^admin/[A-Za-z0-9-_/]*$ admin/index.php?rt=$1 [L] 

# RewriteRule for root ajax file 
RewriteRule ^ajax/[A-Za-z0-9-_/]*$ index-ajax.php?rt=$1 [L] 

# RewriteRule for root file - by here, it is not ajax or admin related, so only 
# possible option left if the root index file 
RewriteRule ^[A-Za-z0-9-_/]*$ index.php?rt=$1 [L] 

我做了一個簡單的網站有兩個文件夾 - 「根」和「根/管理員」,而且每個的內部的CSS,圖片,和一些虛擬內容的JavaScript文件夾。在'root'和'root/admin'裏面有一個index.php和index-ajax.php文件,可以簡單地輸出任何url參數,並且使用來自每個文件夾的css,js和image文件。

我現在的問題是,如果我做一個像索引/等等或/管理/索引/等等的網址,那麼該網頁提出正確的參數是正確的。然而,當我做了像索引/等/視圖或管理/索引/等/視圖的網址然後爭論是正確的(?rt = blah /視圖),但頁面呈現錯誤,因爲css/js/images文件去索引/ blah/[css]而不是索引/ [css]。

有關如何處理此問題的任何想法?我允許css/js/image文件通過.htaccess原樣通過,因此在那裏的工作量會減少。

回答

0

所以圖像/ CSS /等打破,因爲你在HTML中使用相對URL,而現在你會更嵌套他們重新打破?在我遇到的一些其他MVC設置中,通常會有某種「根路徑」變量,由Controller根據請求URL進行設置,並由View組件用於將所有路徑前綴爲圖像/樣式/腳本等資源。

將你的.htaccess問題變成一個Controller/PHP問題,如果這對你來說是一個可以接受的解決方案,那麼你的Controller會做一些類似於「/」s上的請求URL的計算,計算結果片斷的數量,並從結果中構建一個相對URL。

我爲自定義MVC設置做了類似的事情;請查看http://avogadro.ws/hosted/skel/public/results/school-1並查看CSS位置的源代碼。該頁面的CSS實際上處於http://avogadro.ws/hosted/skel/級別,Controller將其構建爲相對字符串。

2

那麼,它看起來很不錯,除了那些放在中間的$有點兒會毀了它;一個$標記正則表達式的結尾,所以我不認爲它會正常工作。

此外,我認爲您可能需要避開正斜槓,但我不確定。

+0

我只是不明顯鍵入速度不夠明顯。 – seth 2009-07-08 21:50:22

+0

我認爲$符號可作爲佔位符將來自左側網址的值轉移到正確的網址中? – 2009-07-08 21:52:34

+0

他意味着第一部分中的$,/ – seth 2009-07-08 21:53:36

0

最後一行應該有可能是:

RewriteRule ^/([A-Za-z\-_]+)/([A-Za-z\-_]+)/([A-Za-z0-9\-_]+)$ /index.php?com=$1&action=$2&val=$3 [L,R] 
9

你確定這一切都應該由index.php文件進行處理?什麼靜態文件,如圖像/ CSS等?

這是另一種可能令你感興趣的方法。您可以將任何尚不存在的URL作爲文件或目錄轉發到index.php文件,然後解析其中的URL,例如, [domain.com]/cms/view/15將被重寫爲[domain.com] /index.php/cms/view/15。對於這項工作,您需要將apache指令AcceptPathInfo設置爲On。

的.htaccess:

RewriteEngine On 
#check url is not a valid file 
RewriteCond %{REQUEST_FILENAME} !-f 
#check url is not a valid directory 
RewriteCond %{REQUEST_FILENAME} !-d 
#rewite anything left 
RewriteRule ^(.*)$ index.php/$1 [L] 

的index.php

$path = trim($_SERVER['PATH_INFO'], '/'); 
$pathParts = explode('/', $path); 

if (isset($pathParts[0])) { 
    $com = $pathParts[0]; 
} else { 
    $com = 'defaultcom'; 
} 

//$com[1] will be 'view' if supplied 
//$com[2] will be 15 if supplied 

我喜歡這種方法,因爲你不會被強迫來定義和理解在Apache配置中的URL,但你可以完成PHP中的大部分工作。

編輯

你可以使用它作爲你的.htaccess代替,這將與重定向列表中沒有你的PHP腳本擴展的任何請求。 RewriteCond應該停止重寫管理文件夾的請求。

RewriteEngine On 
#don't rewrite admin 
RewriteCond %{REQUEST_URI} ^admin/ 
#rewrite anything with a file extension not in the list 
RewriteRule !\.(js|gif|jpg|png|css|ico)$ /index.php [L] 
+0

圖像和CSS也可以由PHP處理。我定期做一些非常愚蠢的事情,當然可以通過重新配置Apache來做到這一點(雖然你可能做不到,但可以):我把一個名爲`static.php`的文件只添加過期頭文件給`static_files /`。 – 2009-07-09 13:32:25

3

我會做like Tom Haigh said和解析與PHP請求的URL路徑:

// extract the URL path 
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); 
// extract the path segments 
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/')); 
// expected parameters and its default values 
$params = array(
    'com' => 'default', 
    'action' => 'default', 
    'val' => 'default' 
); 
foreach ($params as $key => $val) { 
    if (isset($segments[0])) { 
     $_GET[rawurldecode($key)] = rawurldecode(array_shift($segments)); 
    } else { 
     break; 
    } 
} 
$restOfTheURLPath = implode('/', $segments); 
var_dump($_GET); 

而且mod_rewrite規則:

# redirect every request, that cannot be mapped to an existing file, to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^admin(/|$) admin/index.php [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

編輯在回答你的評論和現在編輯的問題:您可以排除目錄你想在前面的是結束改寫處理規則:

RewriteRule ^(css|images)/ - [L] 
RewriteRule ^admin/index\.php$ - [L] 
RewriteRule !^index\.php$ index.php [L] 
3

除非我錯過了你的問題的要點(我在你的問題編輯後投入),這聽起來好像你需要修復多個文件夾深度的URL的相對圖像/ css路徑。簡單的解決方案,不要使用相對圖像路徑!只需用斜槓開始你的圖像/ CSS鏈接..

<img src="/img/logo.png" alt="something" /> 

我一直認爲這是一個更簡單的方法,當你有一個模板供電很多頁這不可預知的文件夾的深度。只要你有本地開發的測試URL,它對本地和生產服務器就能正常工作。使用Linux/Apache服務器

你的目錄和文件設置進行測試時

1
# htaccess file for framework - GOOD 
Options +FollowSymLinks 

# Turn on the mod_rewrite engine - GOOD 
RewriteEngine On 

# Hide indexes - GOOD 
Options -Indexes 

# If a file is not one of these, continue processing. - GOOD 
RewriteRule ^images/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ - 
RewriteRule ^images/admin/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ - 
RewriteRule ^admin/(([A-Za-z0-9\-_/]*)/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ admin/images/$3.$4 [L] 
RewriteRule ^(([A-Za-z0-9\-_/]*)/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ images/$3.$4 [L] 

# RewriteRules for folder index files 
#RewriteRule ^(index)?(.php)?$ index.php [L] - GOOD 
#RewriteRule ^admin(/?)(index)?(.php)?$ admin/index.php [L] - GOOD 

# RewriteRules for admin folder arguements - going from more specific to less 
RewriteRule ^admin/ajax/([A-Za-z0-9\-_/]*)$ admin/index-ajax.php?rt=$1 [L] 
RewriteRule ^admin/([A-Za-z0-9\-_/]*)$ admin/index.php?rt=$1 [L] 

# RewriteRule for root ajax file 
RewriteRule ^ajax/([A-Za-z0-9\-_/]*)$ index-ajax.php?rt=$1 [L] 

# RewriteRule for root file - by here, it is not ajax or admin related, so only 
# possible option left if the root index file 
RewriteRule ^([A-Za-z0-9\-_/]*)$ index.php?rt=$1 [L] 

以上的.htaccess工作我代替你忽略圖像與改寫,使得在/ AJAX的圖像的請求行/你好/世界/圖像去/圖像,如果它在管理文件夾,它將去/管理/圖像