2013-04-10 57 views
0

這不是mod_rewrite的問題,而是我的問題。我很確定我的錯誤是非常愚蠢的,但我無法解決它。 我在Mac OS X Lion上設置了一個非常簡單的XAMPP安裝,它有一個簡單的index.php腳本,它什麼都不做。我只是想了解如何在PHP中使用友好的URL,所以,如果我用這個格式接收的URLapache中的mod_rewrite問題

http://localhost/project/controller_name/action_name/id 

所以,如果我收到這

http://localhost/project/user/delete/7 

可以使用內部像這

http://localhost/project/index.php?controller=user&action=delete&id=7 

我創建的項目文件夾.httacess與此內容

RewriteEngine On 
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /aletta/index.php?controller=$1&action=$2&id=$3 [L] 

我檢查了是否啓用了mode_rewrite並且它是。但不起作用。我輸入 http://localhost/project/user/delete/7,頁面顯示「找不到對象!」

任何幫助將大大appreaciate

+0

你是什麼DOCUMENT_ROOT和你在哪裏以上述規則放置.htaccess? – anubhava 2013-04-10 05:31:53

回答

0

試試這個,放在根目錄.htaccess文件:

Options +FollowSymLinks -MultiViews 

<IfModule mod_rewrite.c> 

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

    # set project dir to RewriteBase 
    RewriteBase /aletta/ 
    RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?/([0-9a-zA-Z\-]+?)?$ index.php?controller=$1&action=$2&id=$3 

</IfModule> 

和運行http://localhost/user/delete/7,在index.php設置var_dump($_GET);和結果將是:

array (size=3) 
    'controller' => string 'user' (length=4) 
    'action' => string 'delete' (length=6) 
    'id' => string '7' (length=1) 
0

如果要重新映射/project/$ alphanum1/$ alphanum2/$ numeric進入/project/index.php?controller=$alphanum1 &行動= $ alphanum2 & ID = $數字,那麼你可以給這個配置指令一試:

Options +FollowSymlinks 
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^project/([a-z0-9-_]+)/([a-z0-9-_]+)/([0-9]+)/?$ /project/index.php?controller=$1&action=$2&id=$3 [NC]