.htaccess 重写 GET 变量
我有一个index.php,它处理所有路由index.php?page=controller(简化)只是为了将逻辑与视图分开。
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:_\-]+)$ 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
我无法让它与重写规则一起使用。
控制器可能如下所示:
<?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();
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
基本上人们想说的是,你可以像这样制定重写规则:
这将使你的实际 php 文件像这样:
你的实际 URL 会像这样:
在你的 PHP 文件中,你可以通过分解这样来访问你的参数所以:
Basically what people try to say is, you can make a rewrite rule like so:
This will make your actual php file like so:
And your actual URL would be like so:
And in your PHP file you could access your params by exploding this like so:
我认为最好将所有请求重定向到 index.php 文件,然后使用 php 提取控制器名称和任何其他参数。与任何其他框架(例如 Zend Framework)相同。
这是一个简单的课程,可以满足您的需求。
如何使用它:
.htaccess 文件:
I think it's better if you redirect all requests to the index.php file and then extract the controller name and any other parameters using php. Same as any other frameworks such as Zend Framework.
Here is simple class that can do what you are after.
How to use it:
.htaccess file:
您的重写规则将传递整个 URL:
您的 index.php 会将完整路径解释为您的控制器/参数/值/参数/值(我的 PHP 有点生疏):
Your rewrite rule would pass the entire URL:
Your index.php would interpret that full path as controller/param/value/param/value for you (my PHP is a little rusty):
重定向到
index.php?params=param/value/param/value
,并让 php 分割整个$_GET['params']
怎么样?我认为这就是 wordpress 处理它的方式。How about redirect to
index.php?params=param/value/param/value
, and let php split the whole$_GET['params']
? I think this is the way wordpress handling it.由于某种原因,所选的解决方案对我不起作用。它始终只返回“index.php”作为参数值。
经过一番尝试和错误后,我发现以下规则效果很好。假设您希望 yoursite.com/somewhere/var1/var2/var3 指向 yoursite.com/somewhere/index.php?params=var1/var2/var3,然后将以下规则放入“somewhere”中的 .htaccess 文件中目录:
然后,在 PHP 或您选择的任何语言中,只需使用爆炸命令分隔值,如 @Wesso 所指出的。
出于测试目的,这在您的 index.php 文件中应该足够了:
For some reason, the selected solution did not work for me. It would constantly only return "index.php" as value of params.
After some trial and error, I found the following rules to work well. Assuming you want yoursite.com/somewhere/var1/var2/var3 to point to yoursite.com/somewhere/index.php?params=var1/var2/var3, then place the following rule in a .htaccess file in the "somewhere" directory:
Then, in PHP or whichever language of your choice, simply separate the values using the explode command as pointed out by @Wesso.
For testing purposes, this should suffice in your index.php file:
这是您要找的吗?
此示例演示如何使用循环标志轻松隐藏查询字符串参数。假设您的 URL 类似于 http://www.mysite。 com/foo.asp?a=A&b=B&c=C 并且您希望以 http://www.myhost.com/foo.asp/a/A/b/B/c/C
尝试以下规则以获得所需的结果:
RewriteRule ^( .*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
Is this what your looking for?
This example demonstrates how to easily hide query string parameters using loop flag. Suppose you have URL like http://www.mysite.com/foo.asp?a=A&b=B&c=C and you want to access it as http://www.myhost.com/foo.asp/a/A/b/B/c/C
Try the following rule to achieve desired result:
RewriteRule ^(.*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
您确定使用的是 apache 服务器吗?.htaccess 仅适用于 apache 服务器。如果您使用 IIS,则需要 web.config。在这种情况下:
Are you sure you are using apache server,.htaccess works only on apache server. If you are using IIS then web.config is reqired. In that case:
使用 QSA
blabla.php?originalquery=333
RewriteRule ^blabla.php index.php?addnewquery1=111&addnewquery2=222 [L、NC、QSA]
您将获得全部 111,222,333
use QSA
blabla.php?originialquery=333
RewriteRule ^blabla.php index.php?addnewquery1=111&addnewquery2=222 [L,NC,QSA]
you will get all 111,222,333