繁体   English   中英

网址为mod_rewrite的静态链接页面

[英]Url mod_rewrite for static chained pages

如何使用mod_rewrite实现呢?

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

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

还(没有文件夹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

您误解了URL重写应如何进行。 当您使用MVC模式时,您的URL告诉框架/引导程序执行哪个控制器和方法。 因此,您的URL应该看起来类似于: http://host.com/CONTROLLER/ACTION/what/ever : http://host.com/CONTROLLER/ACTION/what/ever 这就是为什么您无法将.com/index.php?menu=home&list=hello重新编写为.com/hello http://host.com/hellocontroller以及在它是action (控制器类的方法),将无法区分。

下面的代码将重写:

  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]

令人头疼的是,为什么不创建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);
?>

顺便说一句,您的第三个例子是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM