2012-11-04 52 views
0

我怎样才能达到这与mod_rewrite?Url mod_rewrite静态链接页

from: 
.com/index.php?menu=home 
to: 
.com/home 

from: 
.com/index.php?menu=home&list=hello 
to: 
.com/home/hello 

ALSO(不带文件夹hierarki)

from: 
.com/index.php?menu=home&list=hello 
to: 
.com/hello 

我用这对第一个:

RewriteRule ^([^/\.]+)/?$ index.php?menu=$1 [L] 

但如何我连接他们,如果有多个变量?

试过这样:

RewriteRule ^([^/]*)/([^/]*)?$ index.php?home=$1&list=$2 
+0

“多变量”是什么意思?如果你正在讨论GET变量,你需要设置'[L,QSA]'这将会把所有的GET参数传递给你的脚本。例如,如果用户转到'.com/home/hello?testing = variable',它会将它传递给页面。这是你要求的吗? – tftd

+0

更新了我的问题。最后一行。 – user1121487

回答

1

您误会了应该如何完成URL重写。当你使用MVC模式时,你的URL会告诉框架/引导程序执行哪个控制器和方法。 因此,您的网址应类似于:http://host.com/CONTROLLER/ACTION/what/ever。这就是为什么您不能将.com/index.php?menu=home&list=hello重写为.com/hello。当http://host.com/hellocontrolleraction(控制器类的方法)时,将无法区分。

下面的代码将改写:

  1. .com/whatever作为.com/index.php?menu=whatever
  2. .com/whatever/youwant作为.com/index.php?menu=whatever&list=youwant
  3. .com/whatever/youwant/with/additional/parameters作为.com/index.php?menu=whatever&list=youwant&additional=$5

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]*)([/])?([^/]*)?([/])?(.*)$ index.php?menu=$1&list=$3&additional=$5 [L,QSA] 
+0

谢谢你,这完美的作品。 – user1121487

0

一大堆头疼的,为什么不创建规则和路线传递给您的脚本,然后使用爆炸()来划分和界定:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA] 
在你的PHP

然后

<?php 
$route = explode('/',$_GET['route']); 

$menu = (!empty($route[0])?:null); 
$list = (!empty($route[1])?:null); 
?> 

BTW你的第3例子是不可能的。

+0

这会干扰MVC结构的网站是建立在恐惧... – user1121487