.htaccess 重写 GET 变量 [英] .htaccess rewrite GET variables
问题描述
我有一个 index.php 来处理所有的路由 index.php?page=controller (simplified) 只是为了将逻辑与视图分开.
I have a index.php which handle all the routing index.php?page=controller (simplified) just to split up the logic with the view.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([wd~%.:_-]+)$ index.php?page=$1 [NC]
基本上:http://localhost/index.php?page=controller到
谁能帮我添加重写
http://localhost/controller/param/value/param/value (以此类推)
那就是:
http://localhost/controller/?param=value¶m=value
我无法让它与 Rewriterule 一起使用.
I can't get it to work with the Rewriterule.
控制器可能如下所示:
<?php
if (isset($_GET['action'])) {
if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>
还有:
<?php
if (isset($_GET['action']) && isset($_GET['x'])) {
if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>
推荐答案
基本上人们想说的是,你可以像这样制定重写规则:
Basically what people try to say is, you can make a rewrite rule like so:
RewriteRule ^(.*)$ index.php?params=$1 [NC, QSA]
这将使您的实际 php 文件像这样:
This will make your actual php file like so:
index.php?params=param/value/param/value
您的实际 URL 如下所示:
And your actual URL would be like so:
http://url.com/params/param/value/param/value
在你的 PHP 文件中,你可以通过像这样分解来访问你的参数:
And in your PHP file you could access your params by exploding this like so:
<?php
$params = explode( "/", $_GET['params'] );
for($i = 0; $i < count($params); $i+=2) {
echo $params[$i] ." has value: ". $params[$i+1] ."<br />";
}
?>
这篇关于.htaccess 重写 GET 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!