Mod_rewrite 平面链接

发布于 2024-11-01 09:33:11 字数 695 浏览 2 评论 0原文

我正在尝试将我的获取变量显示为平面链接。 想要从此更改:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-08 09:33:11

所以首先记住 mod_rewrite 的工作原理是这样的:(

http://mydomain.com/index.php/shop/hat/10

客户端类型)被重写为

http://mydomain.com/index.php?page=shop&var1=hat&var2=10

(客户端所服务的内容),但不一定像后者那样显示。 (除非你将其设置为重定向)

所以假设你的格式在这里完整描述:

RewriteRule ^index\.php/([^/]+)/([^/]+)/([0-9]+)$ /index.php?page=$1&var2=$2&var2=$3

应该很好。

编辑:

哦顺便说一句!我没有考虑变量的数量。我认为这不应该由 mod_rewrite 处理。也许最好的办法是 RewriteRule index.php/(.*) /index.php?call=$1 然后使用脚本使用 / 分隔符进行爆炸。

仅当您已经知道变量的数量时,您才能做到这一点。

So first remember mod_rewrite works like this :

http://mydomain.com/index.php/shop/hat/10

(what the client type) is rewritten to

http://mydomain.com/index.php?page=shop&var1=hat&var2=10

(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 :

RewriteRule ^index\.php/([^/]+)/([^/]+)/([0-9]+)$ /index.php?page=$1&var2=$2&var2=$3

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.

橘亓 2024-11-08 09:33:11

您可以将所有请求直接定向到index.php(称为引导),然后让脚本解析变量。

这就是 Zend Framework 的实现方式,您应该看一下。采埃孚 (ZF) 还拥有许多其他您可以利用的好东西。

这是我的重写块,来自我的一个带有 ZF 支持的网站的虚拟主机。

    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}      !^/favicon.ico
    RewriteRule ^.*$ index.php [NC,L]

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.

    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}      !^/favicon.ico
    RewriteRule ^.*$ index.php [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文