Mod_rewrite 平面链接
我正在尝试将我的获取变量显示为平面链接。 想要从此更改:
http://mydomain.com/index.php?page=shop&var1=hat&var2=10
请
http://mydomain.com/index.php/shop/hat/10
记住,变量的数量没有固定数量,这就是我使用 var1,var2,...等的原因
[编辑] 我已经有一个部分工作的脚本,但仅适用于最多 3 个变量
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3&var3=$4 [L]
I'm trying to display my get variables as flat links.
Want to change from this:
http://mydomain.com/index.php?page=shop&var1=hat&var2=10
to
http://mydomain.com/index.php/shop/hat/10
Please keep in mind that there isn't a fixed number of variables, that's why I use var1,var2,...etc
[edit]
I already have a partially working script, but works only with max 3 variables
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3&var3=$4 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以首先记住 mod_rewrite 的工作原理是这样的:(
客户端类型)被重写为
(客户端所服务的内容),但不一定像后者那样显示。 (除非你将其设置为重定向)
所以假设你的格式在这里完整描述:
应该很好。
编辑:
哦顺便说一句!我没有考虑变量的数量。我认为这不应该由 mod_rewrite 处理。也许最好的办法是
RewriteRule index.php/(.*) /index.php?call=$1
然后使用脚本使用 / 分隔符进行爆炸。仅当您已经知道变量的数量时,您才能做到这一点。
So first remember mod_rewrite works like this :
(what the client type) is rewritten to
(what the client is served), but not necessarily displayed like the latter. (Unless you make it a redirect)
So assuming your format is completly described here :
Should be good.
EDIT:
Oh BTW! I did not take the variable number of variable into account. That should not be processed by mod_rewrite I think. Maybe the best shot is to
RewriteRule index.php/(.*) /index.php?call=$1
and then use your script to explode using / delimiter.You can only do it if you already know the number of variable only AFAIK.
您可以将所有请求直接定向到index.php(称为引导),然后让脚本解析变量。
这就是 Zend Framework 的实现方式,您应该看一下。采埃孚 (ZF) 还拥有许多其他您可以利用的好东西。
这是我的重写块,来自我的一个带有 ZF 支持的网站的虚拟主机。
You could just direct all request directly to index.php (called bootstrapping), and let the script parse out the variables.
This is how it's done with Zend Framework, you should take a look at it. ZF also has loads of other goodies you could utilize.
This is my rewrite block from one of my vhosts with a ZF powered website.