Php 静态链接页面的Url mod_重写
如何使用mod_rewrite实现这一点Php 静态链接页面的Url mod_重写,php,url,mod-rewrite,Php,Url,Mod Rewrite,如何使用mod_rewrite实现这一点 from: .com/index.php?menu=home to: .com/home 及 另外(不带文件夹hierarki) 我用这个做第一个: RewriteRule ^([^/\.]+)/?$ index.php?menu=$1 [L] 但是如果有多个变量,我如何连接它们呢 我试过这个: RewriteRule ^([^/]*)/([^/]*)?$ index.php?home=$1&list=$2 有很多麻烦事,为什么不创建1规
from:
.com/index.php?menu=home
to:
.com/home
及
另外(不带文件夹hierarki)
我用这个做第一个:
RewriteRule ^([^/\.]+)/?$ index.php?menu=$1 [L]
但是如果有多个变量,我如何连接它们呢
我试过这个:
RewriteRule ^([^/]*)/([^/]*)?$ index.php?home=$1&list=$2
有很多麻烦事,为什么不创建1规则并将路由传递给脚本,然后使用explode()拆分和定义:
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);
?>
顺便说一句,你的第三个例子是不可能的。你误解了URL重写应该如何完成。当您使用
MVC
模式时,您的URL会告诉框架/引导程序要执行哪个控制器和方法。
因此,您的URL应该类似于:http://host.com/CONTROLLER/ACTION/what/ever
。这就是为什么不能将.com/index.php?menu=home&list=hello
重新编写为.com/hello
。当http://host.com/hello
是一个控制器
,当它是一个动作时
(控制器类的方法)
下面的代码将重写:
.com/whatever
作为.com/index.php?菜单=whatever
.com/whatever/youwant
作为.com/index.php?菜单=whatever&list=youwant
.com/whatever/youwant/with/additional/parameters
as.com/index.php?menu=whatever&list=youwant&additional=$5
您所说的多变量是什么意思?如果您谈论的是GET变量,那么需要设置
[L,QSA]
,这将把所有GET参数传递给脚本。例如,如果用户转到.com/home/hello?testing=variable
,它将把它传递到页面。这就是你要问的吗?更新了我的问题。最后一行。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
<?php
$route = explode('/',$_GET['route']);
$menu = (!empty($route[0])?:null);
$list = (!empty($route[1])?:null);
?>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)([/])?([^/]*)?([/])?(.*)$ index.php?menu=$1&list=$3&additional=$5 [L,QSA]