2013-04-10 61 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]