我无法理解我想要使用mod_rewrite做什么。
我希望能够输入一个网址,例如:
http://site.com/project/project-title/people/alex-coady
或
http://site.com/project/project-title/tasks/task-list-title
然后将使用可用变量在handle.php
处理,以便:
$_GET['project'] would equal 'project-title'
$_GET['people'] would equal 'alex-coady'
(第一个例子)
$_GET['tasks'] would equal 'task-list-title'
(第二个例子)
重申:所有请求都将由handle.php
管理,但如果在网址上添加了任何其他变量,则首先是关键字people
,tasks
,projects
(将检查我手动添加的任何其他内容,并且紧接在它们之后的值将以上面建议的形式添加。
即。 http://site.com/handle.php?project=project-title&people=alex-coady&tasks=
(第一个例子)
谢谢。
答案 0 :(得分:1)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([^/]+)/([^/]+)/([^/]+)$ handle.php?project=$1&people=$2&tasks=$3 [L]
或者
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/(.*) handle.php?data=$1 [L]
并在handle.php
list($project, $task, $people, $foo, $bar) = explode('/', $_GET['data]);
答案 1 :(得分:1)
我如何处理这个问题就是忘记项目,人员,任务并将所有内容传递给handle.php,然后在那里处理它,否则你最终可能会为将来添加的每个请求设置一个重写规则。 / p>
所以你的mod_rewrite看起来像是:
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ handle.php?route=$1 [L,QSA]
你的句柄/路由器看起来像这样(例子)
<?php
//Get Route from request
$route = (!isset($_GET['route']))?'':$_GET['route'];
/*Split the parts of the route*/
$parts = explode('/', $route,4);
//http://site.com/project/project-title/people/alex-coady
//http://site.com/project/project-title/tasks/task-list-title
$project = (isset($parts[0]))?$parts[0]:null; //project/
$p_title = (isset($parts[1]))?$parts[1]:null; //project-title/
$action = (isset($parts[2]))?$parts[2]:null; //people or tasks
$sub_action = (isset($parts[3]))?$parts[3]:null; //alex-coady or task-list-title
//Then work with the above variables
?>