.htaccess 重写 GET 变量

编程入门 行业动态 更新时间:2024-10-15 14:14:23
本文介绍了.htaccess 重写 GET 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 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]

基本上:localhost/index.php?page=controller到

localhost/controller/

谁能帮我添加重写

localhost/controller/param/value/param/value (以此类推)

那就是:

localhost/controller/?param=value&param=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:

url/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 变量

本文发布于:2023-10-11 14:13:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   变量   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!